home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S1.ZIP / src / libgplus.5 / libgplus / utils / etags.c < prev    next >
C/C++ Source or Header  |  1993-06-22  |  68KB  |  3,029 lines

  1. /* Tags file maker to go with GNU Emacs
  2.    Copyright (C) 1984, 1987, 1988, 1989, 1992 Free Software Foundation, Inc. and Ken Arnold
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * Authors:
  22.  *    Ctags originally by Ken Arnold.
  23.  *    FORTRAN added by Jim Kleckner.
  24.  *    Ed Pelegri-Llopart added C typedefs.
  25.  *    Gnu Emacs TAGS format and modifications by RMS?
  26.  *    Sam Kendall added C++.
  27.  */
  28.  
  29. /* AIX requires this to be the first thing in the file. */
  30. #ifdef    __GNUC__
  31. #define    alloca    __builtin_alloca
  32. #else
  33. #ifdef    sparc
  34. #include <alloca.h>
  35. #else
  36. #ifdef _AIX
  37.  #pragma alloca
  38. #else
  39. extern char *alloca ();
  40. #endif
  41. #endif
  42. #endif
  43.  
  44. #include <stdio.h>
  45. #include <ctype.h>
  46. #include <sys/types.h>
  47. #include <sys/stat.h>
  48.  
  49. #include "getopt.h"
  50.  
  51. extern char *malloc (), *realloc ();
  52. extern char *getenv ();
  53. extern char *index (), *rindex ();
  54. extern char *strcpy (), *strncpy ();
  55. extern int strcmp ();
  56.  
  57. #ifdef hpux
  58. #define notdef
  59. #endif
  60.  
  61. /* Define the symbol ETAGS to make the program "etags",
  62.  which makes emacs-style tag tables by default.
  63.  Define CTAGS to make the program "ctags" compatible with the usual one.
  64.  Define neither one to get behavior that depends
  65.  on the name with which the program is invoked
  66.  (but we don't normally compile it that way).  */
  67.  
  68. #if !defined(ETAGS) && !defined(CTAGS)
  69. /* If neither is defined, program can be run as either. */
  70. #define ETAGS
  71. #define CTAGS
  72. #endif
  73.  
  74. /* On VMS, CTAGS is not useful, so always do ETAGS.  */
  75. #ifdef VMS
  76. #ifndef ETAGS
  77. #define ETAGS
  78. #endif
  79. #endif
  80.  
  81. /* Exit codes for success and failure.  */
  82. #ifdef VMS
  83. #define    GOOD    (1)
  84. #define BAD    (0)
  85. #else
  86. #define    GOOD    (0)
  87. #define    BAD    (1)
  88. #endif
  89.  
  90. /*
  91.  * The FILEPOS abstract type, which represents a position in a file,
  92.  * plus the following accessor functions:
  93.  *
  94.  *    long GET_CHARNO (pos)
  95.  *                returns absolute char number.
  96.  *    long GET_COOKIE (pos)
  97.  *                returns ftell () cookie.
  98.  *    void SET_FILEPOS (pos, fp, charno)
  99.  *        FILE *fp; long charno;
  100.  *                sets `pos' from the current file
  101.  *                position of `fp' and from `charno',
  102.  *                which must be the absolute character
  103.  *                number corresponding to the current
  104.  *                position of `fp'.
  105.  *
  106.  * The `pos' parameter is an lvalue expression of type FILEPOS.
  107.  * Parameters to the accessor functions are evaluated 0 or more times,
  108.  * and so must have no side effects.
  109.  *
  110.  * FILEPOS objects can also be assigned and passed to and from
  111.  * functions in the normal C manner.
  112.  *
  113.  * Implementation notes: the `+ 0' is to enforce rvalue-ness.
  114.  */
  115. #ifdef VMS
  116. typedef struct
  117. {
  118.   long cookie;
  119.   long charno;
  120. } FILEPOS;
  121.  
  122. #define GET_CHARNO(pos)    ((pos).charno + 0)
  123. #define GET_COOKIE(pos)    ((pos).cookie + 0)
  124. #define SET_FILEPOS(pos, fp, cno) \
  125.     ((void) ((pos).cookie = ftell (fp), (pos).charno = (cno)))
  126. #else
  127. #ifndef DEBUG
  128.  /* UNIX real implementation */
  129. typedef long FILEPOS;
  130. #define GET_CHARNO(pos)    ((pos) + 0)
  131. #define GET_COOKIE(pos)    GET_CHARNO (pos)
  132. #define SET_FILEPOS(pos, fp, cno)    ((void) ((pos) = (cno)))
  133. #else
  134.  /* UNIX debugging implementation */
  135. typedef struct
  136. {
  137.   long charno;
  138. } FILEPOS;
  139.  
  140. #define GET_CHARNO(pos)    ((pos).charno + 0)
  141. #define GET_COOKIE(pos)    GET_CHARNO (pos)
  142. #define SET_FILEPOS(pos, fp, cno)                    \
  143.     ((void) ((pos).charno = (cno),                    \
  144.          (cno) != ftell (fp) ? (error ("SET_FILEPOS inconsistency"), 0) \
  145.                       : 0))
  146. #endif
  147. #endif
  148.  
  149. #define streq(s, t)    (strcmp (s, t) == 0)
  150. #define strneq(s, t, n)    (strncmp (s, t, n) == 0)
  151. #define    reg    register
  152. #define    logical    char
  153.  
  154. #define    TRUE    1
  155. #define    FALSE    0
  156.  
  157. #define    iswhite(arg)    (_wht[arg])    /* T if char is white        */
  158. #define    begtoken(arg)    (_btk[arg])    /* T if char can start token    */
  159. #define    intoken(arg)    (_itk[arg])    /* T if char can be in token    */
  160. #define    endtoken(arg)    (_etk[arg])    /* T if char ends tokens    */
  161. #define    isgood(arg)    (_gd[arg])    /* T if char can be after ')'    */
  162.  
  163. #define    max(I1,I2)    ((I1) > (I2) ? (I1) : (I2))
  164.  
  165. struct nd_st
  166. {                /* sorting structure            */
  167.   char *name;            /* function or type name    */
  168.   char *file;            /* file name            */
  169.   logical is_func;        /* use pattern or line no    */
  170.   logical rewritten;        /* list name separately        */
  171.   logical been_warned;        /* set if noticed dup        */
  172.   int lno;            /* line number tag is on    */
  173.   long cno;            /* character number line starts on */
  174.   char *pat;            /* search pattern        */
  175.   struct nd_st *left, *right;    /* left and right sons        */
  176. };
  177.  
  178. long ftell ();
  179. typedef struct nd_st NODE;
  180.  
  181. logical gotone,            /* found a func already on line    */
  182.  /* boolean "func" (see init)    */
  183.   header_file,            /* TRUE if .h file, FALSE o.w.  */
  184.   _wht[0177], _etk[0177], _itk[0177], _btk[0177], _gd[0177];
  185.  
  186.  
  187. char *concat ();
  188. char *savenstr ();
  189. char *savestr ();
  190. char *xmalloc ();
  191. char *xrealloc ();
  192. int L_isdef ();
  193. int PF_funcs ();
  194. int total_size_of_entries ();
  195. logical consider_token ();
  196. logical tail ();
  197. long readline ();
  198. void Asm_funcs ();
  199. void C_entries ();
  200. void L_funcs ();
  201. void L_getit ();
  202. void PAS_funcs ();
  203. void Scheme_funcs ();
  204. void TEX_funcs ();
  205. void add_node ();
  206. void error ();
  207. void fatal ();
  208. void find_entries ();
  209. void free_tree ();
  210. void getit ();
  211. void getline ();
  212. void init ();
  213. void initbuffer ();
  214. void initbuffer ();
  215. void pfnote ();
  216. void process_file ();
  217. void put_entries ();
  218. void takeprec ();
  219.  
  220. /*
  221.  * MACRO
  222.  *    xnew -- allocate storage
  223.  *
  224.  * SYNOPSIS
  225.  *    Type *xnew (int n, Type);
  226.  */
  227. #define xnew(n, Type)    ((Type *) xmalloc ((n) * sizeof (Type)))
  228.  
  229.  
  230.  
  231. /*
  232.  *    Symbol table stuff.
  233.  *
  234.  * Should probably be implemented with hash table; linked list for now.
  235.  */
  236.  
  237. enum sym_type
  238. {
  239.   st_none, st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
  240. };
  241.  
  242. struct stab_entry
  243. {
  244.   char *sym;
  245.   int symlen;
  246.   enum sym_type type;
  247.   struct stab_entry *next;
  248. };
  249.  
  250. typedef struct stab_entry Stab_entry;
  251. typedef Stab_entry *Stab;
  252.  
  253. /*
  254.  * NAME
  255.  *    Stab, Stab_entry, stab_create, stab_search, stab_find -- symbol table
  256.  *
  257.  * SYNOPSIS
  258.  *    Types: Stab, Stab_entry, enum sym_type
  259.  *
  260.  *    Stab * stab_create ()
  261.  *
  262.  *    Stab_entry * stab_find (stab, sym)
  263.  *    Stab *stab;
  264.  *    char *sym;
  265.  *
  266.  *    Stab_entry * stab_search (stab, sym)
  267.  *    Stab *stab;
  268.  *    char *sym;
  269.  *
  270.  * DESCRIPTION
  271.  *    stab_create creates a Stab, a symbol table object, and returns a
  272.  *    pointer to it.  stab_find finds a symbol in a Stab; it returns a
  273.  *    pointer to the Stab_entry if found, otherwise NULL.  stab_search
  274.  *    is like stab_find, except that it creates a new Stab_entry,
  275.  *    initialized with type = st_none, if one did not exist already
  276.  *    (it never returns NULL).
  277.  *
  278.  *    A Stab_entry is a structure that contains at least the following
  279.  *    members:
  280.  *
  281.  *        char *name;        // must not be modified
  282.  *        enum sym_type type;    // should be set
  283.  *
  284.  *    The type field is initially set to st_none; it should be set to
  285.  *    something else by the caller of stab_search.  Other possible values
  286.  *    of an enum sym_type can be added.
  287.  */
  288.  
  289. Stab *
  290. stab_create ()
  291. {
  292.   Stab *sp;
  293.   sp = xnew (1, Stab);
  294.   *sp = NULL;            /* a Stab starts out as a null Stab_entry* */
  295.   return sp;
  296. }
  297.  
  298. Stab_entry *
  299. stab_find (stab, sym, symlen)
  300.      Stab *stab;
  301.      register char *sym;
  302.      register int symlen;
  303. {
  304.   register Stab_entry *se;
  305.   for (se = *stab; se != NULL; se = se->next)
  306.     {
  307.       if (se->symlen == symlen && strneq (se->sym, sym, symlen))
  308.     return se;
  309.     }
  310.  
  311.   return NULL;
  312. }
  313.  
  314. Stab_entry *
  315. stab_search (stab, sym, symlen)
  316.      register Stab *stab;
  317.      char *sym;
  318.      int symlen;
  319. {
  320.   register Stab_entry *se;
  321.   se = stab_find (stab, sym, symlen);
  322.  
  323.   if (se == NULL)
  324.     {
  325.       /* make a new one */
  326.       se = xnew (1, Stab_entry);
  327.       se->sym = savenstr (sym, symlen);
  328.       se->symlen = symlen;
  329.       se->type = st_none;
  330.       se->next = *stab;
  331.       *stab = se;
  332.     }
  333.  
  334.   return se;
  335. }
  336.  
  337. /*
  338.  * NAME
  339.  *    stab_type -- type of a symbol table entry
  340.  *
  341.  * SYNOPSIS
  342.  *    enum sym_type stab_type (Stab_entry *se);
  343.  *
  344.  * WARNING
  345.  *    May evaluate its argument more than once.
  346.  */
  347.  
  348. #define stab_type(se)    ((se)==NULL ? st_none : (se)->type)
  349.  
  350.  
  351.  
  352. typedef int LINENO;
  353.  
  354. typedef struct
  355. {
  356.   char *p;
  357.   int len;
  358.   FILEPOS linestart;
  359.   LINENO lineno;
  360.   logical rewritten;
  361. } TOKEN;
  362.  
  363.  
  364.  /* typedefs are recognized using a simple finite automaton.
  365.   * tydef is its state variable.
  366.   */
  367. typedef enum
  368. {
  369.   none, begin, middle, end
  370. } TYST;
  371.  
  372. TYST tydef = none;
  373.  
  374.  
  375.  /* struct tags for C++ are recognized using another simple
  376.   * finite automaton.  `structdef' is its state variable.
  377.   * This machinery is only invoked for C++; otherwise structdef
  378.   * should remain snone.  However, this machinery can easily be
  379.   * adapted to find structure tags in normal C code.
  380.   */
  381. typedef enum
  382. {
  383.   snone,            /* nothing seen yet */
  384.   skeyseen,            /* struct-like keyword seen */
  385.   stagseen,            /* struct-like tag seen */
  386.   scolonseen,            /* colon seen after struct-like tag */
  387.   sinbody            /* in a class body: recognize member func defs */
  388. } STRUCTST;
  389. STRUCTST structdef = snone;
  390. /*
  391.  * When structdef is stagseen, scolonseen, or sinbody, structtag is the
  392.  * struct tag, and structkey is the preceding struct-like keyword.
  393.  */
  394. char structtag[512];
  395. Stab_entry *structkey;
  396.  
  397. /*
  398.  * Yet another little state machine to deal with preprocessor lines.
  399.  */
  400. typedef enum
  401. {
  402.   dnone,            /* nothing seen */
  403.   dsharpseen,            /* '#' seen as first char on line */
  404.   ddefineseen,            /* '#' and 'define' seen */
  405.   dignorerest            /* ignore rest of line */
  406. } DEFINEST;
  407. DEFINEST definedef;
  408.  
  409. /*
  410.  * LEVEL_OK_FOR_FUNCDEF allows C++ function definition within class body.
  411.  * Currently tydef and structdef stuff (typedefs and struct definitions) are
  412.  * only noticed when level==0, but that may change.
  413.  *
  414.  * Note that this macro may only be evaluated inside C_entries().  It is
  415.  * for self-documentation only.
  416.  */
  417. #define LEVEL_OK_FOR_FUNCDEF()                    \
  418.     (level==0 || (c_ext && level==1 && structdef==sinbody))
  419.  
  420. /*
  421.  * next_token_is_func
  422.  *    set this to TRUE, and the next token considered is called a function.
  423.  */
  424. logical next_token_is_func;
  425.  
  426. /* C extensions.  Currently all listed extensions are C++ dialects, so
  427.  * `c_ext' is used as an abbreviation for `c_ext&C_PLPL'.  If a non-C++
  428.  * dialect is added, this must change.
  429.  */
  430. #define C_PLPL    0x1        /* C++ */
  431. #define C_STAR    0x3        /* C* */
  432.  
  433. char searchar = '/';        /* use /.../ searches         */
  434.  
  435. LINENO lineno;            /* line number of current line */
  436. long charno;            /* current character number */
  437. FILEPOS linepos;        /* start of line (C only) */
  438. FILEPOS prev_linepos;        /* start of previous line (C only) */
  439.  
  440. long linecharno;        /* charno of start of line; not used by C, but
  441.                  * by every other language.
  442.                  */
  443.  
  444. char *curfile,            /* current input file name        */
  445.  *outfile,            /* output file                */
  446.  *white = " \f\t\n",        /* white chars                */
  447.  *endtk = " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?",    /* token ending chars            */
  448.  *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$",    /* token starting chars            */
  449.  *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789",    /* valid in-token chars            */
  450.  *notgd = ",;";            /* non-valid after-function chars    */
  451.  
  452. int append_to_tagfile;        /* -a: append to tags */
  453. int emacs_tags_format;        /* emacs style output (no -e option any more) */
  454. /* The following three default to 1 for etags, but to 0 for ctags.  */
  455. int typedefs;            /* -t: create tags for typedefs */
  456. int typedefs_and_cplusplus;    /* -T: create tags for typedefs, level */
  457.                 /* 0 struct/enum/union decls, and C++ */
  458.                 /* member functions */
  459. int constantypedefs;        /* -d: create tags for C #define and enum */
  460.                 /* constants. Default under etags.  Enum */
  461.                 /* constants not implemented. */
  462.                 /* -D: opposite of -d.  Default under ctags. */
  463. int update;            /* -u: update tags */
  464. int vgrind_style;        /* -v: create vgrind style index output */
  465. int no_warnings;        /* -w: suppress warnings */
  466. int cxref_style;        /* -x: create cxref style output */
  467. int cplusplus;            /* .[hc] means C++, not C */
  468. int noindentypedefs;        /* -S: ignore indentation in C */
  469.  
  470. /* Name this program was invoked with.  */
  471. char *progname;
  472.  
  473. struct option longopts[] = {
  474.   { "append",            no_argument,       NULL, 'a' },
  475.   { "backward-search",        no_argument,       NULL, 'B' }, 
  476.   { "c++",            no_argument,       NULL, 'C' },
  477.   { "cxref",            no_argument,       NULL, 'x' },
  478.   { "defines",            no_argument,       NULL, 'd' },
  479.   { "forward-search",        no_argument,       NULL, 'F' }, 
  480.   { "help",            no_argument,       NULL, 'H' },
  481.   { "ignore-indentation",    no_argument,       NULL, 'S' },
  482.   { "include",            required_argument, NULL, 'i' },
  483.   { "no-defines",        no_argument,       NULL, 'D' },
  484.   { "no-warn",            no_argument,       NULL, 'w' },
  485.   { "output",            required_argument, NULL, 'o' },
  486.   { "typedefs",            no_argument,       NULL, 't' },
  487.   { "typedefs-and-c++",        no_argument,       NULL, 'T' },
  488.   { "update",            no_argument,       NULL, 'u' }, 
  489.   { "version",            no_argument,       NULL, 'V' },
  490.   { "vgrind",            no_argument,       NULL, 'v' }, 
  491.   { 0 }
  492. };
  493.  
  494. FILE *inf,            /* ioptr for current input file        */
  495.  *outf;                /* ioptr for tags file            */
  496.  
  497. NODE *head;            /* the head of the binary tree of tags    */
  498.  
  499. int permit_duplicates = 1;    /* Nonzero means allow duplicate tags.  */
  500.  
  501. /* A `struct linebuffer' is a structure which holds a line of text.
  502.  `readline' reads a line from a stream into a linebuffer
  503.  and works regardless of the length of the line.  */
  504.  
  505. struct linebuffer
  506. {
  507.   long size;
  508.   char *buffer;
  509. };
  510.  
  511. struct linebuffer lb;        /* the current line */
  512. struct linebuffer lb1;        /* sometimes, a previous line in which a token lies */
  513. struct linebuffer filename_lb;    /* used to read in filenames */
  514.  
  515.  
  516. void
  517. print_version ()
  518. {
  519. #ifdef CTAGS
  520.   printf ("CTAGS ");
  521. #ifdef ETAGS
  522.   printf ("and ");
  523. #endif
  524. #endif
  525. #ifdef ETAGS
  526.   printf ("ETAGS ");
  527. #endif
  528.   printf ("for Emacs version 19.0.\n");
  529.  
  530.   exit (0);
  531. }
  532.  
  533. void
  534. print_help ()
  535. {
  536.   printf ("These are the options accepted by %s.  You may use unambiguous\n\
  537. abbreviations for the long option names.\n\n", progname);
  538.  
  539.   fputs ("\
  540. -a, --append\n\
  541.         Append tag entries to existing tags file.\n\
  542. -C, --c++\n\
  543.         Treat files with `.c' and `.h' extensions as C++ code, not C\n\
  544.         code.  Files with `.C', `.H', `.cxx', `.hxx', or `.cc'\n\
  545.         extensions are always assumed to be C++ code.\n\
  546. -d, --defines\n\
  547.         Create tag entries for #defines, too.", stdout);
  548.  
  549. #ifdef ETAGS
  550.   fputs ("  This is the default\n\
  551.         behavior.", stdout);
  552. #endif
  553.  
  554.   fputs ("\n\
  555. -D, --no-defines\n\
  556.         Don't create tag entries for #defines.", stdout);
  557.  
  558. #ifdef CTAGS
  559.   fputs ("  This is the default\n\
  560.         behavior.", stdout);
  561. #endif
  562.  
  563.   puts ("\n\
  564. -o FILE, --output=FILE\n\
  565.         Write the tags to FILE.\n\
  566. -S, --ignore-indentation\n\
  567.         Don't rely on indentation quite as much as normal.  Currently,\n\
  568.         this means not to assume that a closing brace in the first\n\
  569.         column is the final brace of a function or structure\n\
  570.         definition.\n\
  571. -t, --typedefs\n\
  572.         Generate tag entries for typedefs.  This is the default\n\
  573.         behavior.\n\
  574. -T, --typedefs-and-c++\n\
  575.         Generate tag entries for typedefs, struct/enum/union tags, and\n\
  576.         C++ member functions.");
  577.  
  578. #ifdef ETAGS
  579.   puts ("-i FILE, --include=FILE\n\
  580.         Include a note in tag file indicating that, when searching for\n\
  581.         a tag, one should also consult the tags file FILE after\n\
  582.         checking the current file.");
  583. #endif
  584.  
  585. #ifdef CTAGS
  586.   puts ("-B, --backward-search\n\
  587.         Write the search commands for the tag entries using '?', the\n\
  588.         backward-search command.\n\
  589. -F, --forward-search\n\
  590.         Write the search commands for the tag entries using '/', the\n\
  591.         forward-search command.\n\
  592. -u, --update\n\
  593.         Update the tag entries for the given files, leaving tag\n\
  594.         entries for other files in place.  Currently, this is\n\
  595.         implemented by deleting the existing entries for the given\n\
  596.         files and then rewriting the new entries at the end of the\n\
  597.         tags file.  It is often faster to simply rebuild the entire\n\
  598.         tag file than to use this.\n\
  599. -v, --vgrind\n\
  600.         Generates an index of items intended for human consumption,\n\
  601.         similar to the output of vgrind.  The index is sorted, and\n\
  602.         gives the page number of each item.\n\
  603. -x, --cxref\n\
  604.         Like --vgrind, but in the style of cxref, rather than vgrind.\n\
  605.         The output uses line numbers instead of page numbers, but\n\
  606.         beyond that the differences are cosmetic; try both to see\n\
  607.         which you like.\n\
  608. -w, --no-warn\n\
  609.         Suppress warning messages about entries defined in multiple\n\
  610.         files.");
  611. #endif
  612.  
  613.   puts ("-V, --version\n\
  614.         Print the version of the program.\n\
  615. -H, --help\n\
  616.         Print this help message.");
  617.  
  618.   exit (0);
  619. }
  620.  
  621.  
  622. void
  623. main (argc, argv)
  624.      int argc;
  625.      char *argv[];
  626. {
  627.   char cmd[100];
  628.   int i;
  629.   unsigned int nincluded_files = 0;
  630.   char **included_files = (char **) alloca (argc * sizeof (char *));
  631.   char *this_file;
  632. #ifdef VMS
  633.   char got_err;
  634.  
  635.   extern char *gfnames ();
  636.   extern char *massage_name ();
  637. #endif
  638.  
  639.   progname = argv[0];
  640.  
  641. #ifndef CTAGS
  642.   emacs_tags_format = 1;
  643. #else
  644.   emacs_tags_format = 0;
  645. #endif
  646.  
  647.   /*
  648.    * If etags, always find typedefs and structure tags.  Why not?
  649.    * Also default is to find macro constants.
  650.    */
  651.   if (emacs_tags_format)
  652.     typedefs = typedefs_and_cplusplus = constantypedefs = 1;
  653.  
  654.   for (;;)
  655.     {
  656.       int opt;
  657.       opt = getopt_long (argc, argv, "aCdDo:StTi:BFuvxwVH", longopts, 0);
  658.  
  659.       if (opt == EOF)
  660.     break;
  661.  
  662.       switch (opt)
  663.     {
  664.     case '\0':
  665.       /* If getopt returns '\0', then it has already processed a
  666.          long-named option.  We should do nothing.  */
  667.       break;
  668.  
  669.       /* Common options. */
  670.     case 'a':
  671.       append_to_tagfile++;
  672.       break;
  673.     case 'C':
  674.       cplusplus = 1;
  675.       break;
  676.     case 'd':
  677.       constantypedefs = 1;
  678.       break;
  679.     case 'D':
  680.       constantypedefs = 0;
  681.       break;
  682.     case 'o':
  683.       if (outfile)
  684.         {
  685.           fprintf (stderr,
  686.                "%s: -o flag may only be given once\n", progname);
  687.           goto usage;
  688.         }
  689.       outfile = optarg;
  690.       break;
  691.     case 'S':
  692.       noindentypedefs++;
  693.       break;
  694.     case 't':
  695.       typedefs++;
  696.       break;
  697.     case 'T':
  698.       typedefs++;
  699.       typedefs_and_cplusplus++;
  700.       break;
  701.     case 'V':
  702.       print_version ();
  703.       break;
  704.     case 'H':
  705.       print_help ();
  706.       break;
  707.  
  708.       /* Etags options */
  709.     case 'i':
  710.       if (!emacs_tags_format)
  711.         goto usage;
  712.       included_files[nincluded_files++] = optarg;
  713.       break;
  714.  
  715.       /* Ctags options. */
  716.     case 'B':
  717.       searchar = '?';
  718.       if (emacs_tags_format)
  719.         goto usage;
  720.       break;
  721.     case 'F':
  722.       searchar = '/';
  723.       if (emacs_tags_format)
  724.         goto usage;
  725.       break;
  726.     case 'u':
  727.       update++;
  728.       if (emacs_tags_format)
  729.         goto usage;
  730.       break;
  731.     case 'v':
  732.       vgrind_style++;
  733.       /*FALLTHRU*/
  734.     case 'x':
  735.       cxref_style++;
  736.       if (emacs_tags_format)
  737.         goto usage;
  738.       break;
  739.     case 'w':
  740.       no_warnings++;
  741.       if (emacs_tags_format)
  742.         goto usage;
  743.       break;
  744.  
  745.     default:
  746.       goto usage;
  747.     }
  748.     }
  749.  
  750.   if (optind == argc)
  751.     {
  752.       fprintf (stderr, "%s: No input files specified.\n", progname);
  753.  
  754.     usage:
  755.       fprintf (stderr, "%s: Try '%s --help' for a complete list of options.\n",
  756.            progname, progname);
  757.       exit (BAD);
  758.     }
  759.  
  760.   if (outfile == 0)
  761.     {
  762.       outfile = emacs_tags_format ? "TAGS" : "tags";
  763.     }
  764.  
  765.   init ();            /* set up boolean "functions"        */
  766.  
  767.   initbuffer (&lb);
  768.   initbuffer (&lb1);
  769.   initbuffer (&filename_lb);
  770.   /*
  771.    * loop through files finding functions
  772.    */
  773.   if (emacs_tags_format)
  774.     {
  775.       if (streq (outfile, "-"))
  776.     outf = stdout;
  777.       else
  778.     outf = fopen (outfile, append_to_tagfile ? "a" : "w");
  779.       if (!outf)
  780.     {
  781.       perror (outfile);
  782.       exit (1);
  783.     }
  784.     }
  785.  
  786. #ifdef VMS
  787.   argc -= optind;
  788.   argv += optind;
  789.   while (gfnames (&argc, &argv, &got_err) != NULL)
  790.     {
  791.       if (got_err)
  792.     {
  793.       error ("Can't find file %s\n", this_file);
  794.       argc--, argv++;
  795.     }
  796.       else
  797.     {
  798.       this_file = massage_name (this_file);
  799. #if 0
  800.     }
  801.     }                /* solely to balance out the ifdef'd parens above */
  802. #endif
  803. #else
  804.   for (; optind < argc; optind++)
  805.     {
  806.       this_file = argv[optind];
  807.       if (1)
  808.     {
  809. #endif
  810.       /* Input file named "-" means read file names from stdin
  811.          and use them.  */
  812.       if (streq (this_file, "-"))
  813.         {
  814.           while (!feof (stdin))
  815.         {
  816.           (void) readline (&filename_lb, stdin);
  817.           if (strlen (filename_lb.buffer) > 0)
  818.             process_file (filename_lb.buffer);
  819.         }
  820.         }
  821.       else
  822.         process_file (this_file);
  823.     }
  824.     }
  825.  
  826.   if (emacs_tags_format)
  827.     {
  828.       while (nincluded_files-- > 0)
  829.     fprintf (outf, "\f\n%s,include\n", *included_files++);
  830.  
  831.       (void) fclose (outf);
  832.       exit (0);
  833.     }
  834.  
  835.   if (cxref_style)
  836.     {
  837.       put_entries (head);
  838.       exit (GOOD);
  839.     }
  840.   if (update)
  841.     {
  842.       /* update cannot be set under VMS, so we may assume that argc
  843.      and argv have not been munged.  */
  844.       for (i = optind; i < argc; i++)
  845.     {
  846.       sprintf (cmd,
  847.            "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
  848.            outfile, argv[i], outfile);
  849.       (void) system (cmd);
  850.     }
  851.       append_to_tagfile++;
  852.     }
  853.   outf = fopen (outfile, append_to_tagfile ? "a" : "w");
  854.   if (outf == NULL)
  855.     {
  856.       perror (outfile);
  857.       exit (GOOD);
  858.     }
  859.   put_entries (head);
  860.   (void) fclose (outf);
  861.   if (update)
  862.     {
  863.       sprintf (cmd, "sort %s -o %s", outfile, outfile);
  864.       (void) system (cmd);
  865.     }
  866.   exit (GOOD);
  867. }
  868.  
  869.  
  870. /*
  871.  * This routine is called on each file argument.
  872.  */
  873. void
  874. process_file (file)
  875.      char *file;
  876. {
  877.   struct stat stat_buf;
  878.  
  879.   stat (file, &stat_buf);
  880. #ifndef S_ISREG
  881. #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
  882. #endif
  883. #if !defined(S_ISLNK) && defined(S_IFLNK)
  884. #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
  885. #endif
  886.   if (!S_ISREG(stat_buf.st_mode)
  887. #ifdef S_ISLNK
  888.       && !S_ISLNK(stat_buf.st_mode)
  889. #endif
  890.       )
  891.     {
  892.       fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
  893.       return;
  894.     }
  895.  
  896.   if (streq (file, outfile) && !streq (outfile, "-"))
  897.     {
  898.       fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
  899.       return;
  900.     }
  901.   if (emacs_tags_format)
  902.     {
  903.       char *cp = rindex (file, '/');
  904.       if (cp)
  905.     ++cp;
  906.       else
  907.     cp = file;
  908.     }
  909.   find_entries (file);
  910.   if (emacs_tags_format)
  911.     {
  912.       fprintf (outf, "\f\n%s,%d\n",
  913.            file, total_size_of_entries (head));
  914.       put_entries (head);
  915.       free_tree (head);
  916.       head = NULL;
  917.     }
  918. }
  919.  
  920. /*
  921.  * This routine sets up the boolean psuedo-functions which work
  922.  * by seting boolean flags dependent upon the corresponding character
  923.  * Every char which is NOT in that string is not a white char.  Therefore,
  924.  * all of the array "_wht" is set to FALSE, and then the elements
  925.  * subscripted by the chars in "white" are set to TRUE.  Thus "_wht"
  926.  * of a char is TRUE if it is the string "white", else FALSE.
  927.  */
  928. void
  929. init ()
  930. {
  931.   reg char *sp;
  932.   reg int i;
  933.  
  934.   for (i = 0; i < 0177; i++)
  935.     {
  936.       _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
  937.       _gd[i] = TRUE;
  938.     }
  939.   for (sp = white; *sp; sp++)
  940.     _wht[*sp] = TRUE;
  941.   for (sp = endtk; *sp; sp++)
  942.     _etk[*sp] = TRUE;
  943.   for (sp = intk; *sp; sp++)
  944.     _itk[*sp] = TRUE;
  945.   for (sp = begtk; *sp; sp++)
  946.     _btk[*sp] = TRUE;
  947.   for (sp = notgd; *sp; sp++)
  948.     _gd[*sp] = FALSE;
  949.   _wht[0] = _wht['\n'];
  950.   _etk[0] = _etk['\n'];
  951.   _btk[0] = _btk['\n'];
  952.   _itk[0] = _itk['\n'];
  953.   _gd[0] = _gd['\n'];
  954. }
  955.  
  956. /*
  957.  * This routine opens the specified file and calls the function
  958.  * which finds the function and type definitions.
  959.  */
  960. void
  961. find_entries (file)
  962.      char *file;
  963. {
  964.   char *cp;
  965.   void prolog_funcs ();
  966.  
  967.   inf = fopen (file, "r");
  968.   if (inf == NULL)
  969.     {
  970.       perror (file);
  971.       return;
  972.     }
  973.   curfile = savestr (file);
  974.   cp = rindex (file, '.');
  975.  
  976.   header_file = (cp && (streq (cp + 1, "h")));
  977.  
  978.   /* .tex, .aux or .bbl implies LaTeX source code */
  979.   if (cp && (streq (cp + 1, "tex") || streq (cp + 1, "aux")
  980.          || streq (cp + 1, "bbl")))
  981.     {
  982.       TEX_funcs (inf);
  983.       goto close_and_return;
  984.     }
  985.   /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
  986.   if (cp && (streq (cp + 1, "l")
  987.          || streq (cp + 1, "el")
  988.          || streq (cp + 1, "lsp")
  989.          || streq (cp + 1, "lisp")
  990.          || streq (cp + 1, "cl")
  991.          || streq (cp + 1, "clisp")))
  992.     {
  993.       L_funcs (inf);
  994.       goto close_and_return;
  995.     }
  996.   /* .scm or .sm or .scheme or ... implies scheme source code */
  997.   if (cp && (streq (cp + 1, "sm")
  998.          || streq (cp + 1, "scm")
  999.          || streq (cp + 1, "scheme")
  1000.          || streq (cp + 1, "t")
  1001.          || streq (cp + 1, "sch")
  1002.          || streq (cp + 1, "SM")
  1003.          || streq (cp + 1, "SCM")
  1004.          /* The `SCM' or `scm' prefix with a version number */
  1005.              || (cp[-1] == 'm' && cp[-2] == 'c' && cp[-3] == 's'
  1006.          && string_numeric_p (cp + 1))
  1007.              || (cp[-1] == 'M' && cp[-2] == 'C' && cp[-3] == 'S'
  1008.          && string_numeric_p (cp + 1))))
  1009.     {
  1010.       Scheme_funcs (inf);
  1011.       fclose (inf);
  1012.       return;
  1013.     }
  1014.   /* Assume that ".s" or ".a" is assembly code. -wolfgang.  */
  1015.   if (cp && (cp[1] == 's' || cp[1] == 'a') && cp[2] == '\0')
  1016.     {
  1017.       Asm_funcs (inf);
  1018.       fclose (inf);
  1019.       return;
  1020.     }
  1021.   /* .C or .H or .cxx or .hxx or .cc: a C++ file */
  1022.   if (cp && (streq (cp + 1, "C")
  1023.          || streq (cp + 1, "H")
  1024.          || streq (cp + 1, "cxx")
  1025.          || streq (cp + 1, "hxx")
  1026.          || streq (cp + 1, "cc")))
  1027.     {
  1028.       C_entries (C_PLPL);    /* C++ */
  1029.       goto close_and_return;
  1030.     }
  1031.   /* .cs or .hs: a C* file */
  1032.   if (cp && (cp[1] == 'c' || cp[1] == 'h') && cp[2] == 's' && cp[3] == '\0')
  1033.     {
  1034.       C_entries (C_STAR);
  1035.       goto close_and_return;
  1036.     }
  1037.   /* .pl implies prolog source code */
  1038.   if (cp && !strcmp (cp + 1, "pl"))
  1039.     {
  1040.       prolog_funcs (inf);
  1041.       goto close_and_return;
  1042.     }
  1043.   /* .p or .pas: a Pascal file */
  1044.   if (cp && (streq (cp + 1, "p")
  1045.          || streq (cp + 1, "pas")))
  1046.     {
  1047.       PAS_funcs (inf);
  1048.       goto close_and_return;
  1049.     }
  1050.   /* if not a .c or .h or .y file, try fortran */
  1051.   else if (cp && ((cp[1] != 'c'
  1052.            && cp[1] != 'h'
  1053.            && cp[1] != 'y')
  1054.           || (cp[1] != 0 && cp[2] != 0)))
  1055.     {
  1056.       if (PF_funcs (inf) != 0)
  1057.     goto close_and_return;
  1058.       rewind (inf);        /* no fortran tags found, try C */
  1059.     }
  1060.   C_entries (cplusplus ? C_PLPL : 0);
  1061.  
  1062. close_and_return:
  1063.   (void) fclose (inf);
  1064. }
  1065.  
  1066. /* Nonzero if string STR is composed of digits.  */
  1067.  
  1068. int
  1069. string_numeric_p (str)
  1070.      char *str;
  1071. {
  1072.   while (*str)
  1073.     {
  1074.       if (*str < '0' || *str > '9')
  1075.     return 0;
  1076.     }
  1077.   return 1;
  1078. }
  1079.  
  1080. /* Record a tag. */
  1081. /* Should take a TOKEN* instead!! */
  1082.  
  1083. void
  1084. pfnote (name, is_func, rewritten, linestart, linelen, lno, cno)
  1085.      char *name;        /* tag name */
  1086.      logical is_func;        /* function or type name? */
  1087.      logical rewritten;        /* tag different from text of definition? */
  1088.      char *linestart;
  1089.      int linelen;
  1090.      int lno;
  1091.      long cno;
  1092. {
  1093.   register char *fp;
  1094.   register NODE *np;
  1095.   char tem[51];
  1096.   char c;
  1097.  
  1098.   np = (NODE *) malloc (sizeof (NODE));
  1099.   if (np == NULL)
  1100.     {
  1101.       if (!emacs_tags_format)
  1102.     {
  1103.       /* It's okay to output early in etags -- it only disrupts the
  1104.        * character count of the tag entries, which is no longer used
  1105.        * by tags.el anyway.
  1106.        */
  1107.       error ("too many entries to sort");
  1108.     }
  1109.       put_entries (head);
  1110.       free_tree (head);
  1111.       head = NULL;
  1112.       np = xnew (1, NODE);
  1113.     }
  1114.   /* If ctags mode, change name "main" to M<thisfilename>. */
  1115.   if (!emacs_tags_format && !cxref_style && streq (name, "main"))
  1116.     {
  1117.       fp = rindex (curfile, '/');
  1118.       name = concat ("M", fp == 0 ? curfile : fp + 1, "");
  1119.       fp = rindex (name, '.');
  1120.       if (fp && fp[1] != '\0' && fp[2] == '\0')
  1121.     *fp = 0;
  1122.       rewritten = TRUE;
  1123.     }
  1124.   np->name = savestr (name);
  1125.   np->file = curfile;
  1126.   np->is_func = is_func;
  1127.   np->rewritten = rewritten;
  1128.   np->lno = lno;
  1129.   /* UNCOMMENT THE +1 HERE: */
  1130.   np->cno = cno /* + 1 */ ;    /* our char numbers are 0-base; emacs's are 1-base */
  1131.   np->left = np->right = 0;
  1132.   if (emacs_tags_format)
  1133.     {
  1134.       c = linestart[linelen];
  1135.       linestart[linelen] = 0;
  1136.     }
  1137.   else if (cxref_style == 0)
  1138.     {
  1139.       sprintf (tem, strlen (linestart) < 50 ? "%s$" : "%.50s", linestart);
  1140.       linestart = tem;
  1141.     }
  1142.   np->pat = savestr (linestart);
  1143.   if (emacs_tags_format)
  1144.     {
  1145.       linestart[linelen] = c;
  1146.     }
  1147.  
  1148.   add_node (np, &head);
  1149. }
  1150.  
  1151. /*
  1152.  * free_tree ()
  1153.  *    recurse on left children, iterate on right children.
  1154.  */
  1155. void
  1156. free_tree (node)
  1157.      register NODE *node;
  1158. {
  1159.   while (node)
  1160.     {
  1161.       register NODE *node_right = node->right;
  1162.       free_tree (node->left);
  1163.       free (node->name);
  1164.       free (node->pat);
  1165.       free ((char *) node);
  1166.       node = node_right;
  1167.     }
  1168. }
  1169.  
  1170. /*
  1171.  * add_node ()
  1172.  *    Adds a node to the tree of nodes.  In etags mode, we don't keep
  1173.  *    it sorted; we just keep a linear list.  In ctags mode, maintain
  1174.  *    an ordered tree, with no attempt at balancing.
  1175.  *
  1176.  *    add_node is the only function allowed to add nodes, so it can
  1177.  *    maintain state.
  1178.  */
  1179. void
  1180. add_node (node, cur_node_p)
  1181.      NODE *node, **cur_node_p;
  1182. {
  1183.   register int dif;
  1184.   register NODE *cur_node = *cur_node_p;
  1185.   static NODE *last_node = NULL;/* careful */
  1186.  
  1187.   if (cur_node == NULL)
  1188.     {
  1189.       *cur_node_p = node;
  1190.       last_node = node;
  1191.       return;
  1192.     }
  1193.  
  1194.   if (emacs_tags_format)
  1195.     {
  1196.       /* Etags Mode */
  1197.       if (!last_node)
  1198.     fatal ("internal error in add_node");
  1199.       last_node->right = node;
  1200.       last_node = node;
  1201.     }
  1202.   else
  1203.     {
  1204.       /* Ctags Mode */
  1205.       dif = strcmp (node->name, cur_node->name);
  1206.  
  1207.       /*
  1208.        * If this tag name matches an existing one, then
  1209.        * do not add the node, but maybe print a warning.
  1210.        */
  1211.       if (!dif)
  1212.     {
  1213.       if (node->file == cur_node->file)
  1214.         {
  1215.           if (!no_warnings)
  1216.         {
  1217.           fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
  1218.                node->file, lineno, node->name);
  1219.           fprintf (stderr, "Second entry ignored\n");
  1220.         }
  1221.           return;
  1222.         }
  1223.       if (!cur_node->been_warned && !no_warnings)
  1224.         {
  1225.           fprintf (stderr,
  1226.           "Duplicate entry in files %s and %s: %s (Warning only)\n",
  1227.                node->file, cur_node->file, node->name);
  1228.         }
  1229.       cur_node->been_warned = TRUE;
  1230.       return;
  1231.     }
  1232.  
  1233.       /* Maybe refuse to add duplicate nodes.  */
  1234.       if (!permit_duplicates)
  1235.     {
  1236.       if (!strcmp (node->name, cur_node->name)
  1237.           && !strcmp (node->file, cur_node->file))
  1238.         return;
  1239.     }
  1240.  
  1241.       /* Actually add the node */
  1242.       add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
  1243.     }
  1244. }
  1245.  
  1246. void
  1247. put_entries (node)
  1248.      reg NODE *node;
  1249. {
  1250.   reg char *sp;
  1251.  
  1252.   if (node == NULL)
  1253.     return;
  1254.  
  1255.   /* Output subentries that precede this one */
  1256.   put_entries (node->left);
  1257.  
  1258.   /* Output this entry */
  1259.  
  1260.   if (emacs_tags_format)
  1261.     {
  1262.       if (node->rewritten)
  1263.     {
  1264.       fprintf (outf, "%s\177%s\001%d,%d\n",
  1265.            node->name, node->pat, node->lno, node->cno);
  1266.     }
  1267.       else
  1268.     {
  1269.       fprintf (outf, "%s\177%d,%d\n",
  1270.            node->pat, node->lno, node->cno);
  1271.     }
  1272.     }
  1273.   else if (!cxref_style)
  1274.     {
  1275.       fprintf (outf, "%s\t%s\t",
  1276.            node->name, node->file);
  1277.  
  1278.       if (node->is_func)
  1279.     {            /* a function */
  1280.       putc (searchar, outf);
  1281.       putc ('^', outf);
  1282.  
  1283.       for (sp = node->pat; *sp; sp++)
  1284.         {
  1285.           if (*sp == '\\' || *sp == searchar)
  1286.         putc ('\\', outf);
  1287.           putc (*sp, outf);
  1288.         }
  1289.       putc (searchar, outf);
  1290.     }
  1291.       else
  1292.     {            /* a typedef; text pattern inadequate */
  1293.       fprintf (outf, "%d", node->lno);
  1294.     }
  1295.       putc ('\n', outf);
  1296.     }
  1297.   else if (vgrind_style)
  1298.     fprintf (stdout, "%s %s %d\n",
  1299.          node->name, node->file, (node->lno + 63) / 64);
  1300.   else
  1301.     fprintf (stdout, "%-16s %3d %-16s %s\n",
  1302.          node->name, node->lno, node->file, node->pat);
  1303.  
  1304.   /* Output subentries that follow this one */
  1305.   put_entries (node->right);
  1306. }
  1307.  
  1308. /* Length of a number's decimal representation. */
  1309. int
  1310. number_len (num)
  1311.      long num;
  1312. {
  1313.   int len = 0;
  1314.   if (!num)
  1315.     return 1;
  1316.   for (; num; num /= 10)
  1317.     ++len;
  1318.   return len;
  1319. }
  1320.  
  1321. /*
  1322.  * Return total number of characters that put_entries will output for
  1323.  * the nodes in the subtree of the specified node.  Works only if emacs_tags_format
  1324.  * is set, but called only in that case.  This count is irrelevant with
  1325.  * the new tags.el, but is still supplied for backward compatibility.
  1326.  */
  1327. int
  1328. total_size_of_entries (node)
  1329.      reg NODE *node;
  1330. {
  1331.   reg int total;
  1332.  
  1333.   if (node == NULL)
  1334.     return 0;
  1335.  
  1336.   total = 0;
  1337.   for (; node; node = node->right)
  1338.     {
  1339.       /* Count left subentries. */
  1340.       total += total_size_of_entries (node->left);
  1341.  
  1342.       /* Count this entry */
  1343.       total += strlen (node->pat) + 1;
  1344.       total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
  1345.       if (node->rewritten)
  1346.     total += 1 + strlen (node->name);    /* \001name */
  1347.     }
  1348.  
  1349.   return total;
  1350. }
  1351.  
  1352. /*
  1353.  * The C symbol tables.
  1354.  */
  1355.  
  1356. Stab *C_stab, *C_PLPL_stab, *C_STAR_stab;
  1357.  
  1358. /*
  1359.  * SYNOPSIS
  1360.  *    Stab *get_C_stab (int c_ext);
  1361.  */
  1362. #define get_C_stab(c_ext) ((c_ext&C_STAR) ? C_STAR_stab :        \
  1363.                c_ext ? C_PLPL_stab :            \
  1364.                C_stab)
  1365.  
  1366. void
  1367. add_keyword (stab, sym, type)
  1368.      Stab *stab;
  1369.      char *sym;
  1370.      enum sym_type type;
  1371. {
  1372.   stab_search (stab, sym, strlen (sym))->type = type;
  1373. }
  1374.  
  1375. Stab *
  1376. C_create_stab (c_ext)
  1377.      int c_ext;
  1378. {
  1379.   Stab *stab;
  1380.  
  1381.   stab = stab_create ();
  1382.  
  1383.   /* C, C++ and C* */
  1384.   if (c_ext & C_PLPL)
  1385.     add_keyword (stab, "class", st_C_struct);
  1386.   if (c_ext & C_STAR)
  1387.     add_keyword (stab, "domain", st_C_struct);
  1388.   add_keyword (stab, "union", st_C_struct);
  1389.   add_keyword (stab, "struct", st_C_struct);
  1390.   add_keyword (stab, "enum", st_C_enum);
  1391.   add_keyword (stab, "typedef", st_C_typedef);
  1392.   add_keyword (stab, "define", st_C_define);
  1393.   add_keyword (stab, "long", st_C_typespec);
  1394.   add_keyword (stab, "short", st_C_typespec);
  1395.   add_keyword (stab, "int", st_C_typespec);
  1396.   add_keyword (stab, "char", st_C_typespec);
  1397.   add_keyword (stab, "float", st_C_typespec);
  1398.   add_keyword (stab, "double", st_C_typespec);
  1399.   add_keyword (stab, "signed", st_C_typespec);
  1400.   add_keyword (stab, "unsigned", st_C_typespec);
  1401.   add_keyword (stab, "const", st_C_typespec);
  1402.   add_keyword (stab, "volatile", st_C_typespec);
  1403.  
  1404.   return stab;
  1405. }
  1406.  
  1407. void
  1408. C_create_stabs ()
  1409. {
  1410.   C_stab = C_create_stab (0);
  1411.   C_PLPL_stab = C_create_stab (C_PLPL);
  1412.   C_STAR_stab = C_create_stab (C_STAR | C_PLPL);
  1413. }
  1414.  
  1415. /*
  1416.  * C_entries ()
  1417.  *    This routine finds functions and typedefs in C syntax and adds them
  1418.  *    to the list.
  1419.  */
  1420.  
  1421. #define CNL_SAVE_DEFINEDEF                        \
  1422. {                                    \
  1423.   prev_linepos = linepos;                        \
  1424.   SET_FILEPOS (linepos, inf, charno);                    \
  1425.   lineno++;                                \
  1426.   charno += readline (&lb, inf);                    \
  1427.   lp = lb.buffer;                            \
  1428. }
  1429.  
  1430. #define CNL                                \
  1431. {                                    \
  1432.   CNL_SAVE_DEFINEDEF;                            \
  1433.   definedef = dnone;                            \
  1434. }
  1435.  
  1436. void
  1437. C_entries (c_ext)
  1438.      int c_ext;            /* extension of C? */
  1439. {
  1440.   register int c;        /* latest char read; '\0' for end of line */
  1441.   register int tokoff;        /* offset in line of beginning of latest token */
  1442.   register int toklen;        /* length of latest token */
  1443.   register char *lp;        /* pointer one beyond the character `c' */
  1444.   logical incomm, inquote, inchar, midtoken;
  1445.   int level;            /* current curly brace level */
  1446.   char tokb[BUFSIZ];
  1447.  
  1448.   lineno = 0;
  1449.   charno = 0;
  1450.   lp = lb.buffer;
  1451.   *lp = 0;
  1452.  
  1453.   definedef = dnone;
  1454.   gotone = midtoken = inquote = inchar = incomm = FALSE;
  1455.   level = 0;
  1456.   tydef = none;
  1457.   next_token_is_func = 0;
  1458.  
  1459.   C_create_stabs ();
  1460.  
  1461.   while (!feof (inf))
  1462.     {
  1463.       c = *lp++;
  1464.       if (c == '\\')
  1465.     {
  1466.       /* If we're at the end of the line, the next character is a
  1467.          '\0'; don't skip it, because it's the thing that tells us
  1468.          to read the next line.  */
  1469.       if (*lp == 0)
  1470.         continue;
  1471.       lp++;
  1472.       c = ' ';
  1473.     }
  1474.       else if (incomm)
  1475.     {
  1476.       if (c == '*' && *lp == '/')
  1477.         {
  1478.           c = *lp++;
  1479.           incomm = FALSE;
  1480.         }
  1481.     }
  1482.       else if (inquote)
  1483.     {
  1484.       if (c == '"')
  1485.         inquote = FALSE;
  1486.       else if (c == '\\')
  1487.         c = *lp++;
  1488.     }
  1489.       else if (inchar)
  1490.     {
  1491.       if (c == '\'')
  1492.         inchar = FALSE;
  1493.       continue;
  1494.     }
  1495.       else
  1496.     switch (c)
  1497.       {
  1498.       case '"':
  1499.         inquote = TRUE;
  1500.         continue;
  1501.       case '\'':
  1502.         inchar = TRUE;
  1503.         continue;
  1504.       case '/':
  1505.         if (*lp == '*')
  1506.           {
  1507.         lp++;
  1508.         incomm = TRUE;
  1509.           }
  1510.         else if (c_ext && *lp == '/')
  1511.           {
  1512.         c = 0;
  1513.         break;
  1514.           }
  1515.         continue;
  1516.       case '#':
  1517.         if (lp == lb.buffer + 1 && definedef == dnone)
  1518.           definedef = dsharpseen;
  1519.         continue;
  1520.  
  1521.         /*
  1522.          * The next two are to help the strucdef state machine.
  1523.          * They break when they are finished, so they don't interfere
  1524.          * with anything else that is going on.
  1525.          */
  1526.       case ':':
  1527.         if (structdef == stagseen)
  1528.           structdef = scolonseen;
  1529.         break;
  1530.         /* Not a struct definition when semicolon seen in non-sinbody context. */
  1531.       case ';':
  1532.         if (structdef != snone && structdef != sinbody)
  1533.           {
  1534.         structdef = snone;
  1535.         (void) strcpy (structtag, "<error 1>");
  1536.           }
  1537.         break;
  1538.  
  1539.       case '{':
  1540.         if (tydef == begin)
  1541.           {
  1542.         tydef = middle;
  1543.           }
  1544.         switch (structdef)
  1545.           {
  1546.           case skeyseen:    /* unnamed struct */
  1547.         structtag[0] = '\0';
  1548.         /* FALLTHRU */
  1549.           case stagseen:
  1550.           case scolonseen:    /* named struct */
  1551.         structdef = sinbody;
  1552.         break;
  1553.           }
  1554.         level++;
  1555.         continue;
  1556.       case '}':
  1557.         if (!noindentypedefs && lp == lb.buffer + 1)
  1558.           level = 0;    /* reset level if first column */
  1559.         else if (level > 0)
  1560.           level--;
  1561.         if (level == 0 && tydef == middle)
  1562.           {
  1563.         tydef = end;
  1564.           }
  1565.         if (level == 0)
  1566.           {
  1567.         structdef = snone;
  1568.         (void) strcpy (structtag, "<error 2>");
  1569.           }
  1570.         continue;
  1571.       }
  1572.       if (LEVEL_OK_FOR_FUNCDEF () && !inquote && !incomm && gotone == FALSE)
  1573.     {
  1574.       if (midtoken)
  1575.         {
  1576.           if (endtoken (c))
  1577.         {
  1578.           if (c_ext && c == ':' && *lp == ':' && intoken (*(lp + 1)))
  1579.             {
  1580.               /*
  1581.                * This handles :: in the middle, but not at beginning
  1582.                * of an identifier.
  1583.                */
  1584.               lp += 2;
  1585.               toklen += 3;
  1586.             }
  1587.           else
  1588.             {
  1589.               /* The following is no longer true,
  1590.              now that we advance to the next line
  1591.              at the end of processing the character.  */
  1592.               /*
  1593.                * We've just finished lexing an identifier.
  1594.                * Note that if `c' is '\0', `lb' is the NEXT
  1595.                * line, `lp' points to the beginning of it, and
  1596.                * old pointers into `lb.buffer' may no longer be
  1597.                * valid, since `lb.buffer' may have been
  1598.                * reallocated.  In this case (which corresponds
  1599.                * to an identifier followed immediately by a
  1600.                * newline), we re-read the line into lb1.
  1601.                *
  1602.                * This would be faster if the previous line's
  1603.                * buffer were always saved.
  1604.                */
  1605.               logical is_func;
  1606.               char *tok_linebuf;
  1607.               TOKEN tok;
  1608.               logical bingo, tok_at_end_of_line;
  1609.               char *lp_tmp;    /* addressable */
  1610.  
  1611. #if 0
  1612.               if (c == '\0')
  1613.             {
  1614.               getline (GET_COOKIE (prev_linepos));
  1615.               tok_linebuf = lb1.buffer;
  1616.               tok_at_end_of_line = TRUE;
  1617.               tok.linestart = prev_linepos;
  1618.               tok.lineno = lineno - 1;
  1619.             }
  1620.               else
  1621. #endif
  1622.             {
  1623.               tok_linebuf = lb.buffer;
  1624.               tok_at_end_of_line = FALSE;
  1625.               tok.linestart = linepos;
  1626.               tok.lineno = lineno;
  1627.             }
  1628.               tok.p = tok_linebuf + tokoff;
  1629.               tok.len = toklen;
  1630.               tok.rewritten = FALSE;
  1631.               lp_tmp = lp;
  1632.               bingo = consider_token (c, &lp_tmp, &tok,
  1633.                           &is_func, c_ext, level);
  1634.               lp = lp_tmp;
  1635.               if (bingo)
  1636.             {
  1637.               if (GET_CHARNO (tok.linestart) != GET_CHARNO (linepos)
  1638.                   && !tok_at_end_of_line)
  1639.                 {
  1640.                   /*
  1641.                    * Resynchronize tok.p to point into the right
  1642.                    * linebuffer.
  1643.                    */
  1644.                   getline (GET_COOKIE (tok.linestart));
  1645.                   if (!tok.rewritten)
  1646.                 tok.p = lb1.buffer + (tok.p - tok_linebuf);
  1647.                   tok_linebuf = lb1.buffer;
  1648.                 }
  1649.               if (structdef == sinbody && definedef == dnone && is_func)
  1650.                 {    /* function defined in C++ class body */
  1651.                   sprintf (tokb, "%s::%.*s",
  1652.                        structtag[0] == '\0' ? "_anonymous_"
  1653.                        : structtag,
  1654.                        tok.len, tok.p);
  1655.                   tok.rewritten = TRUE;
  1656.                 }
  1657.               else
  1658.                 {
  1659.                   sprintf (tokb, "%.*s", tok.len, tok.p);
  1660.                 }
  1661.               pfnote (tokb, is_func, tok.rewritten, tok_linebuf,
  1662.                  tokoff + toklen + (tok_at_end_of_line ? 0 : 1),
  1663.                   tok.lineno, GET_CHARNO (tok.linestart));
  1664.               gotone = is_func;    /* function */
  1665.             }
  1666.               midtoken = FALSE;
  1667.             }
  1668.         }
  1669.           else if (intoken (c))
  1670.         toklen++;
  1671.         }
  1672.       else if (begtoken (c))
  1673.         {
  1674.           tokoff = lp - 1 - lb.buffer;
  1675.           toklen = 1;
  1676.           midtoken = TRUE;
  1677.         }
  1678.     }
  1679.       /* Detect end of line, after having handled the last token on the line.  */
  1680.       if (c == 0)
  1681.     {
  1682.       CNL;
  1683.       gotone = FALSE;
  1684.     }
  1685.       if (c == ';' && tydef == end)    /* clean with typedefs */
  1686.     tydef = none;
  1687.     }
  1688. }
  1689.  
  1690. /*
  1691.  * consider_token ()
  1692.  *    checks to see if the current token is at the start of a
  1693.  *    function, or corresponds to a typedef.  It updates the input
  1694.  *    line pointer *LPP so that the '(' will be in it when it returns.
  1695.  *
  1696.  *    *IS_FUNC gets TRUE iff the token is a function.
  1697.  *    C_EXT is which language we are looking at.
  1698.  *
  1699.  *    In the future we will need some way to adjust where the end of
  1700.  *    the token is; for instance, implementing the C++ keyword
  1701.  *    `operator' properly will adjust the end of the token to be after
  1702.  *    whatever follows `operator'.
  1703.  *
  1704.  * Globals
  1705.  *    structdef    IN OUT
  1706.  *    definedef    IN OUT
  1707.  *    tydef        IN OUT
  1708.  */
  1709.  
  1710. logical
  1711. consider_token (c, lpp, tokp, is_func, c_ext, level)
  1712.      reg char c;        /* IN: first char after the token */
  1713.      char **lpp;        /* IN OUT: *lpp points to 2nd char after the token */
  1714.      reg TOKEN *tokp;        /* IN */
  1715.      logical *is_func;        /* OUT */
  1716.      int c_ext;            /* IN */
  1717.      int level;            /* IN */
  1718. {
  1719.   reg char *lp = *lpp;
  1720.   logical firsttok;        /* TRUE if have seen first token in ()'s */
  1721.   Stab_entry *tokse = stab_find (get_C_stab (c_ext), tokp->p, tokp->len);
  1722.   enum sym_type toktype = stab_type (tokse);
  1723.  
  1724.   *is_func = TRUE;        /* a function */
  1725.  
  1726.   /*
  1727.    * Advance the definedef state machine.  We set `gotone' for good measure;
  1728.    * it's redundant.
  1729.    */
  1730.   switch (definedef)
  1731.     {
  1732.     case dnone:
  1733.       /* We're not on a preprocessor line. */
  1734.       break;
  1735.     case dsharpseen:
  1736.       if (toktype == st_C_define)
  1737.     {
  1738.       definedef = ddefineseen;
  1739.       gotone = FALSE;
  1740.     }
  1741.       else
  1742.     {
  1743.       definedef = dignorerest;
  1744.       gotone = TRUE;
  1745.     }
  1746.       goto badone;
  1747.     case ddefineseen:
  1748.       /*
  1749.        * Make a tag for any macro.
  1750.        * This will flub up if there is a newline immediately following
  1751.        * the macro name.
  1752.        */
  1753.       *is_func = (c == '(');
  1754.       definedef = dignorerest;
  1755.       gotone = TRUE;
  1756.       if (!*is_func && !constantypedefs)
  1757.     goto badone;
  1758.       goto goodone;
  1759.     case dignorerest:
  1760.       goto badone;
  1761.     default:
  1762.       error ("internal error: definedef value");
  1763.     }
  1764.  
  1765.   /*
  1766.    * Skip whitespace and comments after the token.  This loop should
  1767.    * also skip C++ comments.
  1768.    */
  1769.   while (1)
  1770.     {
  1771.       /* At whitespace => skip it.  */
  1772.       if (iswhite (c))
  1773.     {
  1774.       c = *lp++;
  1775.     }
  1776.       /* At a comment => skip to end of comment.  */
  1777.       else if (c == '/' && *lp == '*')
  1778.     {
  1779.       /* If we find a comment, skip it.  */
  1780.       while (!(c == '*' && *lp == '/'))
  1781.         {
  1782.           c = *lp++;
  1783.           if (c == 0)
  1784.         {
  1785.           lp--;
  1786.           break;
  1787.         }
  1788.         }
  1789.       if (c == '*' && *lp == '/')
  1790.         {
  1791.           lp++;        /* lp now points past the '/' */
  1792.           c = *lp++;    /* c is now the --whatever-- after the '/' */
  1793.         }
  1794.     }
  1795.       else
  1796.     break;
  1797.  
  1798.       /* If we arrived at eof or eol, decide which one it is.
  1799.      If it's eol, advance to the next line.  */
  1800.  
  1801.       if (c == 0)
  1802.     {
  1803.       lp--;
  1804.       break;
  1805.     }
  1806.     }
  1807.  
  1808.   /*
  1809.    * If you have custom token types, or when configuration files can
  1810.    * define custom token types, this switch will be larger.
  1811.    */
  1812.   switch (toktype)
  1813.     {
  1814.     case st_C_typedef:
  1815.       if (typedefs)
  1816.     {
  1817.       tydef = begin;
  1818.       goto badone;
  1819.     }
  1820.       break;
  1821.     case st_C_typespec:
  1822.       if (tydef == begin || tydef == end)
  1823.     {
  1824.       tydef = end;
  1825.       goto badone;
  1826.     }
  1827.       break;
  1828.     }
  1829.  
  1830.   /*
  1831.    * This structdef business is currently only invoked when level==0.
  1832.    * It should be recursively invoked whatever the level, and a stack of
  1833.    * states kept, to allow for definitions of structs within structs.
  1834.    *
  1835.    * This structdef business is NOT invoked when we are ctags and the
  1836.    * file is plain C.  This is because a struct tag may have the same
  1837.    * name as another tag, and this loses with ctags.
  1838.    *
  1839.    * This if statement deals with the tydef state machine as follows: if
  1840.    * tydef==begin and token is struct/union/class/enum, goto badone.
  1841.    * All the other code here is for the structdef state machine.
  1842.    */
  1843.   switch (toktype)
  1844.     {
  1845.     case st_C_struct:
  1846.     case st_C_enum:
  1847.       if (tydef == begin || (typedefs_and_cplusplus && level == 0 && structdef == snone))
  1848.     {
  1849.       structdef = skeyseen;
  1850.       structkey = tokse;
  1851.     }
  1852.       goto badone;
  1853.     }
  1854.  
  1855.   if (structdef == skeyseen)
  1856.     {
  1857.       /* If next char is '{' or (for C++) ':', found a structure tag. */
  1858.       if (c == '{' || (c_ext && c == ':'))
  1859.     {
  1860.       /*
  1861.        * We should do this slightly differently for straight C:
  1862.        * instead of defining `tag', as we now do, we should define
  1863.        * `struct tag'.  (Do this only if the find-tag defaulting is
  1864.        * done on a sophisticated per-mode basis, so that if the user
  1865.        * says meta-. anywhere in `struct foo', the default comes out
  1866.        * `struct foo', not `struct' or `foo'.)  This will require
  1867.        * remembering which keyword (struct/union/class/enum) we saw, as a
  1868.        * Stab_entry* -- this will also make it possible to merge the
  1869.        * skeyseen and senumseen states, if we want.
  1870.        */
  1871.       if (stab_type (structkey) == st_C_struct)
  1872.         {
  1873.           (void) strncpy (structtag, tokp->p, tokp->len);
  1874.           structtag[tokp->len] = '\0';    /* for struct/union/class */
  1875.           structdef = stagseen;
  1876.         }
  1877.       else
  1878.         {
  1879.           structtag[0] = '\0';    /* for enum */
  1880.         }
  1881.       *is_func = FALSE;    /* not a function */
  1882.       goto goodone;
  1883.     }
  1884.       else
  1885.     {
  1886.       /* Not a definition: reset structdef */
  1887.       structdef = snone;
  1888.       (void) strcpy (structtag, "<error 3>");
  1889.     }
  1890.       /* Now what?  And how does/should this stuff interact with tydef?? */
  1891.       /* Also maybe reset lp to *lpp for benefit of the function finding code. */
  1892.     }
  1893.   if (tydef == begin)
  1894.     {
  1895.       tydef = end;
  1896.       goto badone;
  1897.     }
  1898.   if (tydef == end)
  1899.     {
  1900.       *is_func = 0;
  1901.       goto goodone;
  1902.     }
  1903.   /* Detect GNUmacs's function-defining macros. */
  1904.   if (definedef == dnone)
  1905.     {
  1906.       if (strneq (tokp->p, "DEF", 3)
  1907.       || strneq (tokp->p, "ENTRY", 5)
  1908.       || strneq (tokp->p, "SYSCALL", 7)
  1909.       || strneq (tokp->p, "PSEUDO", 6))
  1910.     {
  1911.       next_token_is_func = TRUE;
  1912.       goto badone;
  1913.     }
  1914.       else if (strneq (tokp->p, "EXFUN", 5))
  1915.     {
  1916.       next_token_is_func = FALSE;
  1917.       goto badone;
  1918.     }
  1919.     }
  1920.   if (next_token_is_func)
  1921.     {
  1922.       next_token_is_func = FALSE;
  1923.       goto goodone;
  1924.     }
  1925.   if (c != '(')
  1926.     goto badone;
  1927.   firsttok = FALSE;
  1928.   while ((c = *lp++) != ')')
  1929.     {
  1930.       if (c == 0)
  1931.     {
  1932.       lp--;
  1933.       break;
  1934.     }
  1935.       /*
  1936.     * This line used to confuse ctags:
  1937.     *    int    (*oldhup)();
  1938.     * This fixes it. A nonwhite char before the first
  1939.     * token, other than a / (in case of a comment in there)
  1940.     * makes this not a declaration.
  1941.     */
  1942.       if (begtoken (c) || c == '/')
  1943.     firsttok++;
  1944.       else if (!iswhite (c) && !firsttok)
  1945.     goto badone;
  1946.     }
  1947.   while (iswhite (c = *lp++))
  1948.     {
  1949.       if (c == 0)
  1950.     {
  1951.       lp--;
  1952.       break;
  1953.     }
  1954.     }
  1955.   if (!isgood (c))
  1956.     goto badone;
  1957.  
  1958. goodone:
  1959.   *lpp = lp - 1;
  1960.   return TRUE;
  1961.  
  1962. badone:
  1963.   *lpp = lp - 1;
  1964.   return FALSE;
  1965. }
  1966.  
  1967. void
  1968. getline (atcookie)
  1969.      long atcookie;
  1970. {
  1971.   long saveftell = ftell (inf);
  1972.  
  1973.   (void) fseek (inf, atcookie, 0);
  1974.   (void) readline (&lb1, inf);
  1975.   (void) fseek (inf, saveftell, 0);
  1976. }
  1977.  
  1978. /* Fortran parsing */
  1979.  
  1980. char *dbp;
  1981. int pfcnt;
  1982.  
  1983. int
  1984. PF_funcs (fi)
  1985.      FILE *fi;
  1986. {
  1987.   lineno = 0;
  1988.   charno = 0;
  1989.   pfcnt = 0;
  1990.  
  1991.   while (!feof (fi))
  1992.     {
  1993.       lineno++;
  1994.       linecharno = charno;
  1995.       charno += readline (&lb, fi);
  1996.       dbp = lb.buffer;
  1997.       if (*dbp == '%')
  1998.     dbp++;            /* Ratfor escape to fortran */
  1999.       while (isspace (*dbp))
  2000.     dbp++;
  2001.       if (*dbp == 0)
  2002.     continue;
  2003.       switch (*dbp | ' ')
  2004.     {
  2005.     case 'i':
  2006.       if (tail ("integer"))
  2007.         takeprec ();
  2008.       break;
  2009.     case 'r':
  2010.       if (tail ("real"))
  2011.         takeprec ();
  2012.       break;
  2013.     case 'l':
  2014.       if (tail ("logical"))
  2015.         takeprec ();
  2016.       break;
  2017.     case 'c':
  2018.       if (tail ("complex") || tail ("character"))
  2019.         takeprec ();
  2020.       break;
  2021.     case 'd':
  2022.       if (tail ("double"))
  2023.         {
  2024.           while (isspace (*dbp))
  2025.         dbp++;
  2026.           if (*dbp == 0)
  2027.         continue;
  2028.           if (tail ("precision"))
  2029.         break;
  2030.           continue;
  2031.         }
  2032.       break;
  2033.     }
  2034.       while (isspace (*dbp))
  2035.     dbp++;
  2036.       if (*dbp == 0)
  2037.     continue;
  2038.       switch (*dbp | ' ')
  2039.     {
  2040.     case 'f':
  2041.       if (tail ("function"))
  2042.         getit ();
  2043.       continue;
  2044.     case 's':
  2045.       if (tail ("subroutine"))
  2046.         getit ();
  2047.       continue;
  2048.     case 'p':
  2049.       if (tail ("program"))
  2050.         {
  2051.           getit ();
  2052.           continue;
  2053.         }
  2054.       if (tail ("procedure"))
  2055.         getit ();
  2056.       continue;
  2057.     }
  2058.     }
  2059.   return (pfcnt);
  2060. }
  2061.  
  2062. logical
  2063. tail (cp)
  2064.      char *cp;
  2065. {
  2066.   register int len = 0;
  2067.  
  2068.   while (*cp && (*cp & ~' ') == ((*(dbp + len)) & ~' '))
  2069.     cp++, len++;
  2070.   if (*cp == 0)
  2071.     {
  2072.       dbp += len;
  2073.       return (1);
  2074.     }
  2075.   return (0);
  2076. }
  2077.  
  2078. void
  2079. takeprec ()
  2080. {
  2081.   while (isspace (*dbp))
  2082.     dbp++;
  2083.   if (*dbp != '*')
  2084.     return;
  2085.   dbp++;
  2086.   while (isspace (*dbp))
  2087.     dbp++;
  2088.   if (!isdigit (*dbp))
  2089.     {
  2090.       --dbp;            /* force failure */
  2091.       return;
  2092.     }
  2093.   do
  2094.     dbp++;
  2095.   while (isdigit (*dbp));
  2096. }
  2097.  
  2098. void
  2099. getit ()
  2100. {
  2101.   register char *cp;
  2102.   char c;
  2103.   char nambuf[BUFSIZ];
  2104.  
  2105.   while (isspace (*dbp))
  2106.     dbp++;
  2107.   if (*dbp == 0
  2108.       || (!isalpha (*dbp)
  2109.       && *dbp != '_'
  2110.       && *dbp != '$'))
  2111.     return;
  2112.   for (cp = dbp + 1; *cp && (isalpha (*cp) || isdigit (*cp)
  2113.                  || (*cp == '_') || (*cp == '$')); cp++)
  2114.     continue;
  2115.   c = cp[0];
  2116.   cp[0] = 0;
  2117.   (void) strcpy (nambuf, dbp);
  2118.   cp[0] = c;
  2119.   pfnote (nambuf, TRUE, FALSE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  2120.   pfcnt++;
  2121. }
  2122.  
  2123. /* Handle a file of assembler code.  */
  2124.  
  2125. void
  2126. Asm_funcs (fi)
  2127.      FILE *fi;
  2128. {
  2129.   int i;
  2130.   register char c;
  2131.  
  2132.   lineno = 0;
  2133.   charno = 0;
  2134.   pfcnt = 0;
  2135.  
  2136.   while (!feof (fi))
  2137.     {
  2138.       lineno++;
  2139.       linecharno = charno;
  2140.       charno += readline (&lb, fi);
  2141.       dbp = lb.buffer;
  2142.  
  2143.       for (i = 0; ((c = dbp[i]) && !isspace (c)) && (c != ':'); i++)
  2144.     ;
  2145.  
  2146.       if ((i > 0) && (c == ':'))
  2147.     getit ();
  2148.     }
  2149. }
  2150.  
  2151. /* Added by Mosur Mohan, 4/22/88 */
  2152. /* Pascal parsing                */
  2153.  
  2154. #define GET_NEW_LINE \
  2155. { \
  2156.   linecharno = charno; lineno++; \
  2157.   charno += 1 + readline (&lb, inf); \
  2158.   dbp = lb.buffer; \
  2159. }
  2160.  
  2161. /*  Locates tags for procedures & functions.
  2162.  *  Doesn't do any type- or var-definitions.
  2163.  *  It does look for the keyword "extern" or "forward"
  2164.  *  immediately following the procedure statement;
  2165.  *  if found, the tag is skipped.
  2166.  */
  2167.  
  2168. void
  2169. PAS_funcs (fi)
  2170.      FILE *fi;
  2171. {
  2172.   struct linebuffer tline;    /* mostly copied from C_entries */
  2173.   long save_lcno;
  2174.   int save_lineno;
  2175.   char c, *cp;
  2176.   char nambuf[BUFSIZ];
  2177.  
  2178.   logical            /* each of these flags is TRUE iff: */
  2179.     incomm1,            /* point is inside {..} comment */
  2180.     incomm2,            /* point is inside (*..*) comment */
  2181.     inquote,            /* point is inside '..' string */
  2182.     get_tagname,        /* point is after PROCEDURE/FUNCTION */
  2183.   /*   keyword, so next item = potential tag */
  2184.     found_tag,            /* point is after a potential tag */
  2185.     inparms,            /* point is within parameter-list */
  2186.     verify_tag;            /* point has passed the parm-list, so the */
  2187.   /*   next token will determine whether    */
  2188.   /*   this is a FORWARD/EXTERN to be       */
  2189.   /*   ignored, or whether it is a real tag */
  2190.  
  2191.   lineno = 0;
  2192.   charno = 0;
  2193.   dbp = lb.buffer;
  2194.   *dbp = 0;
  2195.   initbuffer (&tline);
  2196.  
  2197.   incomm1 = incomm2 = inquote = FALSE;
  2198.   found_tag = FALSE;        /* have a proc name; check if extern */
  2199.   get_tagname = FALSE;        /* have found "procedure" keyword    */
  2200.   inparms = FALSE;        /* found '(' after "proc"            */
  2201.   verify_tag = FALSE;        /* check if "extern" is ahead        */
  2202.  
  2203.   /* long main loop to get next char */
  2204.   while (!feof (fi))
  2205.     {
  2206.       c = *dbp++;
  2207.       if (c == 0)        /* if end of line */
  2208.     {
  2209.       GET_NEW_LINE;
  2210.       if (*dbp == 0)
  2211.         continue;
  2212.       if (!((found_tag && verify_tag) ||
  2213.         get_tagname))
  2214.         c = *dbp++;        /* only if don't need *dbp pointing */
  2215.       /* to the beginning of the name of  */
  2216.       /* the procedure or function        */
  2217.     }
  2218.       if (incomm1)        /* within { - } comments */
  2219.     {
  2220.       if (c == '}')
  2221.         incomm1 = FALSE;
  2222.       continue;
  2223.     }
  2224.       else if (incomm2)        /* within (* - *) comments */
  2225.     {
  2226.       if (c == '*')
  2227.         {
  2228.           while ((c = *dbp++) == '*')
  2229.         continue;
  2230.           if (c == 0)
  2231.         GET_NEW_LINE;
  2232.           if (c == ')')
  2233.         incomm2 = FALSE;
  2234.         }
  2235.       continue;
  2236.     }
  2237.       else if (inquote)
  2238.     {
  2239.       if (c == '\'')
  2240.         inquote = FALSE;
  2241.       continue;
  2242.     }
  2243.       else
  2244.     switch (c)
  2245.       {
  2246.       case '\'':
  2247.         inquote = TRUE;    /* found first quote */
  2248.         continue;
  2249.       case '{':        /* found open-{-comment */
  2250.         incomm1 = TRUE;
  2251.         continue;
  2252.       case '(':
  2253.         if (*dbp == '*')    /* found open-(*-comment */
  2254.           {
  2255.         incomm2 = TRUE;
  2256.         dbp++;
  2257.           }
  2258.         else if (found_tag)    /* found '(' after tag, i.e., parm-list */
  2259.           inparms = TRUE;
  2260.         continue;
  2261.       case ')':        /* end of parms list */
  2262.         if (inparms)
  2263.           inparms = FALSE;
  2264.         continue;
  2265.       case ';':
  2266.         if ((found_tag) && (!inparms))    /* end of proc or fn stmt */
  2267.           {
  2268.         verify_tag = TRUE;
  2269.         break;
  2270.           }
  2271.         continue;
  2272.       }
  2273.       if ((found_tag) && (verify_tag) && (*dbp != ' '))
  2274.     {
  2275.       /* check if this is an "extern" declaration */
  2276.       if (*dbp == 0)
  2277.         continue;
  2278.       if ((*dbp == 'e') || (*dbp == 'E'))
  2279.         {
  2280.           if (tail ("extern"))    /* superfluous, really! */
  2281.         {
  2282.           found_tag = FALSE;
  2283.           verify_tag = FALSE;
  2284.         }
  2285.         }
  2286.       else if ((*dbp == 'f') || (*dbp == 'F'))
  2287.         {
  2288.           if (tail ("forward"))    /*  check for forward reference */
  2289.         {
  2290.           found_tag = FALSE;
  2291.           verify_tag = FALSE;
  2292.         }
  2293.         }
  2294.       if ((found_tag) && (verify_tag))    /* not external proc, so make tag */
  2295.         {
  2296.           found_tag = FALSE;
  2297.           verify_tag = FALSE;
  2298.           pfnote (nambuf, TRUE, FALSE,
  2299.               tline.buffer, cp - tline.buffer + 1,
  2300.               save_lineno, save_lcno);
  2301.           continue;
  2302.         }
  2303.     }
  2304.       if (get_tagname)        /* grab name of proc or fn */
  2305.     {
  2306.       if (*dbp == 0)
  2307.         continue;
  2308.  
  2309.       /* save all values for later tagging */
  2310.       tline.size = lb.size;
  2311.       strcpy (tline.buffer, lb.buffer);
  2312.       save_lineno = lineno;
  2313.       save_lcno = linecharno;
  2314.  
  2315.       /* grab block name */
  2316.       for (cp = dbp + 1; *cp && (!endtoken (*cp)); cp++)
  2317.         continue;
  2318.       c = cp[0];
  2319.       cp[0] = 0;
  2320.       strcpy (nambuf, dbp);
  2321.       cp[0] = c;
  2322.       dbp = cp;        /* restore dbp to e-o-token */
  2323.       get_tagname = FALSE;
  2324.       found_tag = TRUE;
  2325.       continue;
  2326.  
  2327.       /* and proceed to check for "extern" */
  2328.     }
  2329.       if ((!incomm1) && (!incomm2) && (!inquote) &&
  2330.       (!found_tag) && (!get_tagname))
  2331.     {
  2332.       /* check for proc/fn keywords */
  2333.       switch (c | ' ')
  2334.         {
  2335.         case 'p':
  2336.           if (tail ("rocedure"))    /* c = 'p', dbp has advanced */
  2337.         get_tagname = TRUE;
  2338.           continue;
  2339.         case 'f':
  2340.           if (tail ("unction"))
  2341.         get_tagname = TRUE;
  2342.           continue;
  2343.         }
  2344.     }
  2345.     }                /* while not e-o-f */
  2346. }
  2347.  
  2348. /*
  2349.  * lisp tag functions
  2350.  * just look for (def or (DEF
  2351.  */
  2352.  
  2353. void
  2354. L_funcs (fi)
  2355.      FILE *fi;
  2356. {
  2357.   lineno = 0;
  2358.   charno = 0;
  2359.   pfcnt = 0;
  2360.  
  2361.   while (!feof (fi))
  2362.     {
  2363.       lineno++;
  2364.       linecharno = charno;
  2365.       charno += readline (&lb, fi);
  2366.       dbp = lb.buffer;
  2367.       if (dbp[0] == '(')
  2368.     {
  2369.       if (L_isdef (dbp))
  2370.         {
  2371.           while (!isspace (*dbp))
  2372.         dbp++;
  2373.           while (isspace (*dbp))
  2374.         dbp++;
  2375.           L_getit ();
  2376.         }
  2377.       else
  2378.         {
  2379.           /* Check for (foo::defmumble name-defined ... */
  2380.           while (*dbp && *dbp != ':' && !isspace (*dbp)
  2381.              && *dbp != '(' && *dbp != ')')
  2382.         dbp++;
  2383.           if (*dbp == ':')
  2384.         {
  2385.           while (*dbp == ':')
  2386.             dbp++;
  2387.  
  2388.           if (L_isdef (dbp))
  2389.             {
  2390.               while (!isspace (*dbp))
  2391.             dbp++;
  2392.               while (isspace (*dbp))
  2393.             dbp++;
  2394.               L_getit ();
  2395.             }
  2396.         }
  2397.         }
  2398.     }
  2399.     }
  2400. }
  2401.  
  2402. int
  2403. L_isdef (dbp)
  2404.      char *dbp;
  2405. {
  2406.   return ((dbp[1] == 'D' || dbp[1] == 'd') &&
  2407.       (dbp[2] == 'E' || dbp[2] == 'e') &&
  2408.       (dbp[3] == 'F' || dbp[3] == 'f'));
  2409. }
  2410.  
  2411. void
  2412. L_getit ()
  2413. {
  2414.   register char *cp;
  2415.   char c;
  2416.   char nambuf[BUFSIZ];
  2417.  
  2418.   if (*dbp == 0)
  2419.     return;
  2420.   for (cp = dbp + 1; *cp && *cp != '(' && *cp != ' '; cp++)
  2421.     continue;
  2422.   c = cp[0];
  2423.   cp[0] = 0;
  2424.   (void) strcpy (nambuf, dbp);
  2425.   cp[0] = c;
  2426.   pfnote (nambuf, TRUE, FALSE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  2427.   pfcnt++;
  2428. }
  2429.  
  2430. /*
  2431.  * Scheme tag functions
  2432.  * look for (def... xyzzy
  2433.  * look for (def... (xyzzy
  2434.  * look for (def ... ((...(xyzzy ....
  2435.  * look for (set! xyzzy
  2436.  */
  2437.  
  2438. static void get_scheme ();
  2439.  
  2440. void
  2441. Scheme_funcs (fi)
  2442.      FILE *fi;
  2443. {
  2444.   lineno = 0;
  2445.   charno = 0;
  2446.   pfcnt = 0;
  2447.  
  2448.   while (!feof (fi))
  2449.     {
  2450.       lineno++;
  2451.       linecharno = charno;
  2452.       charno += readline (&lb, fi);
  2453.       dbp = lb.buffer;
  2454.       if (dbp[0] == '(' &&
  2455.       (dbp[1] == 'D' || dbp[1] == 'd') &&
  2456.       (dbp[2] == 'E' || dbp[2] == 'e') &&
  2457.       (dbp[3] == 'F' || dbp[3] == 'f'))
  2458.     {
  2459.       while (!isspace (*dbp))
  2460.         dbp++;
  2461.       /* Skip over open parens and white space */
  2462.       while (*dbp && (isspace (*dbp) || *dbp == '('))
  2463.         dbp++;
  2464.       get_scheme ();
  2465.     }
  2466.       if (dbp[0] == '(' &&
  2467.       (dbp[1] == 'S' || dbp[1] == 's') &&
  2468.       (dbp[2] == 'E' || dbp[2] == 'e') &&
  2469.       (dbp[3] == 'T' || dbp[3] == 't') &&
  2470.       (dbp[4] == '!' || dbp[4] == '!') &&
  2471.       (isspace (dbp[5])))
  2472.     {
  2473.       while (!isspace (*dbp))
  2474.         dbp++;
  2475.       /* Skip over white space */
  2476.       while (isspace (*dbp))
  2477.         dbp++;
  2478.       get_scheme ();
  2479.     }
  2480.     }
  2481. }
  2482.  
  2483. static void
  2484. get_scheme ()
  2485. {
  2486.   register char *cp;
  2487.   char c;
  2488.   char nambuf[BUFSIZ];
  2489.  
  2490.   if (*dbp == 0)
  2491.     return;
  2492.   /* Go till you get to white space or a syntactic break */
  2493.   for (cp = dbp + 1; *cp && *cp != '(' && *cp != ')' && !isspace (*cp); cp++)
  2494.     continue;
  2495.   /* Null terminate the string there. */
  2496.   c = cp[0];
  2497.   cp[0] = 0;
  2498.   /* Copy the string */
  2499.   strcpy (nambuf, dbp);
  2500.   /* Unterminate the string */
  2501.   cp[0] = c;
  2502.   /* Announce the change */
  2503.   pfnote (nambuf, TRUE, FALSE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  2504.   pfcnt++;
  2505. }
  2506.  
  2507. /* Find tags in TeX and LaTeX input files.  */
  2508.  
  2509. /* TEX_toktab is a table of TeX control sequences that define tags.
  2510.    Each TEX_tabent records one such control sequence.
  2511.    CONVERT THIS TO USE THE Stab TYPE!! */
  2512.  
  2513. struct TEX_tabent
  2514. {
  2515.   char *name;
  2516.   int len;
  2517. };
  2518.  
  2519. struct TEX_tabent *TEX_toktab = NULL;    /* Table with tag tokens */
  2520.  
  2521. /* Default set of control sequences to put into TEX_toktab.
  2522.    The value of environment var TEXTAGS is prepended to this.  */
  2523.  
  2524. static char *TEX_defenv =
  2525. ":chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
  2526.  
  2527. void TEX_mode ();
  2528. struct TEX_tabent *TEX_decode_env ();
  2529. void TEX_getit ();
  2530. int TEX_Token ();
  2531.  
  2532. static char TEX_esc = '\\';
  2533. static char TEX_opgrp = '{';
  2534. static char TEX_clgrp = '}';
  2535.  
  2536. /*
  2537.  * TeX/LaTeX scanning loop.
  2538.  */
  2539.  
  2540. void
  2541. TEX_funcs (fi)
  2542.      FILE *fi;
  2543. {
  2544.   char *lasthit;
  2545.  
  2546.   lineno = 0;
  2547.   charno = 0;
  2548.   pfcnt = 0;
  2549.  
  2550.   /* Select either \ or ! as escape character.  */
  2551.   TEX_mode (fi);
  2552.  
  2553.   /* Initialize token table once from environment. */
  2554.   if (!TEX_toktab)
  2555.     TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
  2556.  
  2557.   while (!feof (fi))
  2558.     {
  2559.       lineno++;
  2560.       linecharno = charno;
  2561.       charno += readline (&lb, fi);
  2562.       dbp = lb.buffer;
  2563.       lasthit = dbp;
  2564.  
  2565.       while (!feof (fi))
  2566.     {            /* Scan each line in file */
  2567.       lineno++;
  2568.       linecharno = charno;
  2569.       charno += readline (&lb, fi);
  2570.       dbp = lb.buffer;
  2571.       lasthit = dbp;
  2572.       while (dbp = index (dbp, TEX_esc))    /* Look at each escape in line */
  2573.         {
  2574.           register int i;
  2575.  
  2576.           if (!*(++dbp))
  2577.         break;
  2578.           linecharno += dbp - lasthit;
  2579.           lasthit = dbp;
  2580.           i = TEX_Token (lasthit);
  2581.           if (0 <= i)
  2582.         {
  2583.           TEX_getit (lasthit, TEX_toktab[i].len);
  2584.           break;    /* We only save a line once */
  2585.         }
  2586.         }
  2587.     }
  2588.     }
  2589. }
  2590.  
  2591. #define TEX_LESC '\\'
  2592. #define TEX_SESC '!'
  2593. #define TEX_cmt  '%'
  2594.  
  2595. /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping */
  2596. /* chars accordingly. */
  2597.  
  2598. void
  2599. TEX_mode (f)
  2600.      FILE *f;
  2601. {
  2602.   int c;
  2603.  
  2604.   while ((c = getc (f)) != EOF)
  2605.     {
  2606.       /* Skip to next line if we hit the TeX comment char. */
  2607.       if (c == TEX_cmt)
  2608.     while (c != '\n')
  2609.       c = getc (f);
  2610.       else if (c == TEX_LESC || c == TEX_SESC )
  2611.     break;
  2612.     }
  2613.  
  2614.   if (c == TEX_LESC)
  2615.     {
  2616.       TEX_esc = TEX_LESC;
  2617.       TEX_opgrp = '{';
  2618.       TEX_clgrp = '}';
  2619.     }
  2620.   else
  2621.     {
  2622.       TEX_esc = TEX_SESC;
  2623.       TEX_opgrp = '<';
  2624.       TEX_clgrp = '>';
  2625.     }
  2626.   rewind (f);
  2627. }
  2628.  
  2629. /* Read environment and prepend it to the default string. */
  2630. /* Build token table. */
  2631.  
  2632. struct TEX_tabent *
  2633. TEX_decode_env (evarname, defenv)
  2634.      char *evarname;
  2635.      char *defenv;
  2636. {
  2637.   register char *env, *p;
  2638.   extern char *savenstr (), *index ();
  2639.  
  2640.   struct TEX_tabent *tab;
  2641.   int size, i;
  2642.  
  2643.   /* Append default string to environment. */
  2644.   env = getenv (evarname);
  2645.   if (!env)
  2646.     env = defenv;
  2647.   else
  2648.     env = concat (env, defenv, "");
  2649.  
  2650.   /* Allocate a token table */
  2651.   for (size = 1, p = env; p;)
  2652.     if ((p = index (p, ':')) && *(++p))
  2653.       size++;
  2654.   tab = xnew (size, struct TEX_tabent);
  2655.  
  2656.   /* Unpack environment string into token table. Be careful about */
  2657.   /* zero-length strings (leading ':', "::" and trailing ':') */
  2658.   for (i = 0; *env;)
  2659.     {
  2660.       p = index (env, ':');
  2661.       if (!p)            /* End of environment string. */
  2662.     p = env + strlen (env);
  2663.       if (p - env > 0)
  2664.     {            /* Only non-zero strings. */
  2665.       tab[i].name = savenstr (env, p - env);
  2666.       tab[i].len = strlen (tab[i].name);
  2667.       i++;
  2668.     }
  2669.       if (*p)
  2670.     env = p + 1;
  2671.       else
  2672.     {
  2673.       tab[i].name = NULL;    /* Mark end of table. */
  2674.       tab[i].len = 0;
  2675.       break;
  2676.     }
  2677.     }
  2678.   return tab;
  2679. }
  2680.  
  2681. /* Record a tag defined by a TeX command of length LEN and starting at NAME.
  2682.    The name being defined actually starts at (NAME + LEN + 1).
  2683.    But we seem to include the TeX command in the tag name.  */
  2684.  
  2685. void
  2686. TEX_getit (name, len)
  2687.      char *name;
  2688.      int len;
  2689. {
  2690.   char *p = name + len;
  2691.   char nambuf[BUFSIZ];
  2692.  
  2693.   if (*name == 0)
  2694.     return;
  2695.  
  2696.   /* Let tag name extend to next group close (or end of line) */
  2697.   while (*p && *p != TEX_clgrp)
  2698.     p++;
  2699.   (void) strncpy (nambuf, name, p - name);
  2700.   nambuf[p - name] = 0;
  2701.  
  2702.   pfnote (nambuf, TRUE, FALSE, lb.buffer, strlen (lb.buffer), lineno, linecharno);
  2703.   pfcnt++;
  2704. }
  2705.  
  2706. /* If the text at CP matches one of the tag-defining TeX command names,
  2707.    return the index of that command in TEX_toktab.
  2708.    Otherwise return -1.  */
  2709.  
  2710. /* Keep the capital `T' in `Token' for dumb truncating compilers
  2711.    (this distinguishes it from `TEX_toktab' */
  2712. int
  2713. TEX_Token (cp)
  2714.      char *cp;
  2715. {
  2716.   int i;
  2717.  
  2718.   for (i = 0; TEX_toktab[i].len > 0; i++)
  2719.     if (strncmp (TEX_toktab[i].name, cp, TEX_toktab[i].len) == 0)
  2720.       return i;
  2721.   return -1;
  2722. }
  2723.  
  2724. /* Support for Prolog.  */
  2725.  
  2726. /* whole head (not only functor, but also arguments)
  2727.    is gotten in compound term. */
  2728.  
  2729. void
  2730. prolog_getit (s, lineno, linecharno)
  2731.      char *s;
  2732.      int lineno;
  2733.      long linecharno;
  2734. {
  2735.   char nambuf[BUFSIZ], *save_s, tmpc;
  2736.   int insquote, npar;
  2737.  
  2738.   save_s = s;
  2739.   insquote = FALSE;
  2740.   npar = 0;
  2741.   while (1)
  2742.     {
  2743.       if (*s == '\0')        /* syntax error. */
  2744.     return;
  2745.       else if (insquote && *s == '\'' && *(s + 1) == '\'')
  2746.     s += 2;
  2747.       else if (*s == '\'')
  2748.     {
  2749.       insquote = !insquote;
  2750.       s++;
  2751.     }
  2752.       else if (!insquote && *s == '(')
  2753.     {
  2754.       npar++;
  2755.       s++;
  2756.     }
  2757.       else if (!insquote && *s == ')')
  2758.     {
  2759.       npar--;
  2760.       s++;
  2761.       if (npar == 0)
  2762.         break;
  2763.       else if (npar < 0)    /* syntax error. */
  2764.         return;
  2765.     }
  2766.       else if (!insquote && *s == '.' && (isspace (*(s + 1)) || *(s + 1) == '\0'))
  2767.     {            /* fullstop. */
  2768.       if (npar != 0)    /* syntax error. */
  2769.         return;
  2770.       s++;
  2771.       break;
  2772.     }
  2773.       else
  2774.     s++;
  2775.     }
  2776.   tmpc = *s;
  2777.   *s = '\0';
  2778.   strcpy (nambuf, save_s);
  2779.   *s = tmpc;
  2780.   pfnote (nambuf, TRUE, save_s, strlen (nambuf), lineno, linecharno);
  2781. }
  2782.  
  2783. /* It is assumed that prolog predicate starts from column 0. */
  2784.  
  2785. void
  2786. prolog_funcs (fi)
  2787.      FILE *fi;
  2788. {
  2789.   void skip_comment (), prolog_getit ();
  2790.  
  2791.   lineno = linecharno = charno = 0;
  2792.   while (!feof (fi))
  2793.     {
  2794.       lineno++;
  2795.       linecharno += charno;
  2796.       charno = readline (&lb, fi) + 1;    /* 1 for newline. */
  2797.       dbp = lb.buffer;
  2798.       if (isspace (dbp[0]))    /* not predicate header. */
  2799.     continue;
  2800.       else if (dbp[0] == '%')    /* comment. */
  2801.     continue;
  2802.       else if (dbp[0] == '/' && dbp[1] == '*')    /* comment. */
  2803.     skip_comment (&lb, fi, &lineno, &linecharno);
  2804.       else            /* found. */
  2805.     prolog_getit (dbp, lineno, linecharno);
  2806.     }
  2807. }
  2808.  
  2809. void
  2810. skip_comment (plb, fi, plineno, plinecharno)
  2811.      struct linebuffer *plb;
  2812.      FILE *fi;
  2813.      int *plineno;        /* result */
  2814.      long *plinecharno;        /* result */
  2815. {
  2816.   while (!substr ("*/", plb->buffer))
  2817.     {
  2818.       (*plineno)++;
  2819.       *plinecharno += readline (plb, fi) + 1;
  2820.     }                /* 1 for newline. */
  2821. }
  2822.  
  2823. /* Return TRUE if 'sub' exists somewhere in 's'. */
  2824.  
  2825. int
  2826. substr (sub, s)
  2827.      char *sub;
  2828.      char *s;
  2829. {
  2830.   while (*s && (s = index (s, *sub)))
  2831.     if (prestr (sub, s))
  2832.       return (TRUE);
  2833.     else
  2834.       s++;
  2835.   return (FALSE);
  2836. }
  2837.  
  2838. /* Return TRUE if 'pre' is prefix of string 's'. */
  2839.  
  2840. int
  2841. prestr (pre, s)
  2842.      char *pre;
  2843.      char *s;
  2844. {
  2845.   if (*pre == '\0')
  2846.     return (TRUE);
  2847.   else if (*pre == *s)
  2848.     return (prestr (pre + 1, s + 1));
  2849.   else
  2850.     return (FALSE);
  2851. }
  2852.  
  2853. /* Initialize a linebuffer for use */
  2854.  
  2855. void
  2856. initbuffer (linebuffer)
  2857.      struct linebuffer *linebuffer;
  2858. {
  2859.   linebuffer->size = 200;
  2860.   linebuffer->buffer = xnew (200, char);
  2861. }
  2862.  
  2863. /*
  2864.  * Read a line of text from `stream' into `linebuffer'.
  2865.  * Return the number of characters read from `stream',
  2866.  * which is the length of the line including the newline, if any.
  2867.  */
  2868. long
  2869. readline (linebuffer, stream)
  2870.      struct linebuffer *linebuffer;
  2871.      register FILE *stream;
  2872. {
  2873.   char *buffer = linebuffer->buffer;
  2874.   register char *p = linebuffer->buffer;
  2875.   register char *pend;
  2876.   int newline;            /* 1 if ended with newline, 0 if ended with EOF */
  2877.  
  2878.   pend = p + linebuffer->size;    /* Separate to avoind 386/IX compiler bug.  */
  2879.  
  2880.   while (1)
  2881.     {
  2882.       register int c = getc (stream);
  2883.       if (p == pend)
  2884.     {
  2885.       linebuffer->size *= 2;
  2886.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  2887.       p += buffer - linebuffer->buffer;
  2888.       pend = buffer + linebuffer->size;
  2889.       linebuffer->buffer = buffer;
  2890.     }
  2891.       if (c < 0 || c == '\n')
  2892.     {
  2893.       *p = 0;
  2894.       newline = (c == '\n' ? 1 : 0);
  2895.       break;
  2896.     }
  2897.       *p++ = c;
  2898.     }
  2899.  
  2900.   return p - buffer + newline;
  2901. }
  2902.  
  2903. char *
  2904. savestr (cp)
  2905.      char *cp;
  2906. {
  2907.   return savenstr (cp, strlen (cp));
  2908. }
  2909.  
  2910. char *
  2911. savenstr (cp, len)
  2912.      char *cp;
  2913.      int len;
  2914. {
  2915.   register char *dp;
  2916.  
  2917.   dp = xnew (len + 1, char);
  2918.   (void) strncpy (dp, cp, len);
  2919.   dp[len] = '\0';
  2920.   return dp;
  2921. }
  2922.  
  2923. #ifdef notdef
  2924. /*
  2925.  * Return the ptr in sp at which the character c last
  2926.  * appears; NULL if not found
  2927.  *
  2928.  * Identical to v7 rindex, included for portability.
  2929.  */
  2930.  
  2931. char *
  2932. rindex (sp, c)
  2933.      register char *sp, c;
  2934. {
  2935.   register char *r;
  2936.  
  2937.   r = NULL;
  2938.   do
  2939.     {
  2940.       if (*sp == c)
  2941.     r = sp;
  2942.   } while (*sp++);
  2943.   return (r);
  2944. }
  2945.  
  2946. /*
  2947.  * Return the ptr in sp at which the character c first
  2948.  * appears; NULL if not found
  2949.  *
  2950.  * Identical to v7 index, included for portability.
  2951.  */
  2952.  
  2953. char *
  2954. index (sp, c)
  2955.      register char *sp, c;
  2956. {
  2957.   do
  2958.     {
  2959.       if (*sp == c)
  2960.     return (sp);
  2961.   } while (*sp++);
  2962.   return (NULL);
  2963. }
  2964.  
  2965. #endif /* notdef */
  2966.  
  2967. /* Print error message and exit.  */
  2968.  
  2969. /* VARARGS1 */
  2970. void
  2971. fatal (s1, s2)
  2972.      char *s1, *s2;
  2973. {
  2974.   error (s1, s2);
  2975.   exit (1);
  2976. }
  2977.  
  2978. /* Print error message.  `s1' is printf control string, `s2' is arg for it. */
  2979.  
  2980. /* VARARGS1 */
  2981. void
  2982. error (s1, s2)
  2983.      char *s1, *s2;
  2984. {
  2985.   fprintf (stderr, "%s: ", progname);
  2986.   fprintf (stderr, s1, s2);
  2987.   fprintf (stderr, "\n");
  2988. }
  2989.  
  2990. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  2991.  
  2992. char *
  2993. concat (s1, s2, s3)
  2994.      char *s1, *s2, *s3;
  2995. {
  2996.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  2997.   char *result = xnew (len1 + len2 + len3 + 1, char);
  2998.  
  2999.   (void) strcpy (result, s1);
  3000.   (void) strcpy (result + len1, s2);
  3001.   (void) strcpy (result + len1 + len2, s3);
  3002.   *(result + len1 + len2 + len3) = 0;
  3003.  
  3004.   return result;
  3005. }
  3006.  
  3007. /* Like malloc but get fatal error if memory is exhausted.  */
  3008.  
  3009. char *
  3010. xmalloc (size)
  3011.      int size;
  3012. {
  3013.   char *result = malloc (size);
  3014.   if (!result)
  3015.     fatal ("virtual memory exhausted", 0);
  3016.   return result;
  3017. }
  3018.  
  3019. char *
  3020. xrealloc (ptr, size)
  3021.      char *ptr;
  3022.      int size;
  3023. {
  3024.   char *result = realloc (ptr, size);
  3025.   if (!result)
  3026.     fatal ("virtual memory exhausted");
  3027.   return result;
  3028. }
  3029.